Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server

Find duplicate values in SQL Server

2.80/5 (5 votes)
13 Oct 2011CPOL 58.3K  
How to find duplicate values in SQL Server.

Here’s a handy query for finding duplicates in a table. Suppose you want to find all email addresses in a table that exist more than once:


SQL
SELECT email,
 
COUNT(email) AS NumOccurrences
 
FROM users
 
GROUP BY email
 
HAVING ( COUNT(email) > 1 )

You could also use this technique to find rows that occur exactly once:


SQL
SELECT email
 
FROM users
 
GROUP BY email
 
HAVING ( COUNT(email) = 1 )

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)