My Works, My Ambition, My Dream
Finding Duplicates with SQL
Here’s a handy query for finding duplicates in a table. Suppose you want to find all names in a table that exist more than once:
SELECT name,
COUNT(name) AS namecount
FROM users
GROUP BY name
HAVING ( namecount > 1 )
You could also use this technique to find rows that occur exactly once:
SELECT name
FROM users
GROUP BY name
HAVING ( COUNT(name) = 1 )
Also see How to remove duplicate rows from a table – Microsoft Knowledge base article.