Saturday, October 17, 2009

Oracle: How to avoid duplicate rows in query result while selecting from table

If you select data from one column in a table like this:

select columnName
  from tableName;


columnName
=========
A
A
A
B
B

you would get that columns data from all rows. If you would like to avoid / skip duplicates (and only get the distinct values) you could type:


select distinct columnName
  from tableName;


columnName
=========
A
B




Another approach is:


select columnName, count(*)
  from tableName
  group by columnName;


columnName   count(*)
=========    ========
A            3
B            2





No comments:

Post a Comment