eggbert
eggbert

Reputation: 3233

find unique rows using SQL?

I want to return all the rows from a table which are unique. I.e. if a certain field in two rows contain the same name, that name shouldn't be shown.

Upvotes: 0

Views: 151

Answers (3)

Veve
Veve

Reputation: 6748

Since you want only the uniques names (and not an unique row for every names like you could have with DISTINCT), you have to use a GROUP BY and a HAVING (instead of a WHERE, because your parameter is the result of a function, not a variable) :

SELECT name FROM myTable GROUP BY name HAVING COUNT(name) = 1

Upvotes: 10

Gordon Linoff
Gordon Linoff

Reputation: 1269445

If you want the complete rows, then use row_number() or distinct on:

select distinct on (name) t.*
from table t
order by name;

Upvotes: 0

i486
i486

Reputation: 6564

SELECT DISTINCT column_name FROM table

Upvotes: 0

Related Questions