DISTINCT clause with WHERE

Mohit picture Mohit · Apr 10, 2011 · Viewed 197.2k times · Source

How can I use the DISTINCT clause with WHERE? For example:

SELECT * FROM table WHERE DISTINCT email; -- email is a column name

I want to select all columns from a table with distinct email addresses.

Answer

Adam Matan picture Adam Matan · Apr 10, 2011

If you mean all columns whose email is unique:

SELECT * FROM table WHERE email in
     (SELECT email FROM table GROUP BY email HAVING COUNT(email)=1);