Skip to content
Advertisement

MYSQL show all rows where no duplicates found

If I had a set of data like this:

Bob Johnson
James Peters
Bob Johnson
Clare Burns
Peter Pan
Clare Burns
Peter Pan

What would be the SQL query to display only “James Peters”. So I am not wanting to remove duplicates (DISTINCT command) but rather show all rows where there are no duplicates found.

Advertisement

Answer

You would use group by for this with a having clause:

select t.col1, t.col2
from table t
group by t.col1, t.col2
having count(*) = 1;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement