Click here to Skip to main content
15,798,200 members
Articles / Database Development / SQL Server

Find duplicate values in SQL Server

Rate me:
Please Sign up or sign in to vote.
2.80/5 (5 votes)
13 Oct 2011CPOL 57.5K   1   3
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:

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:

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


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

Written By
Web Developer
India India
Software developer by profession, working for a service and product based organisation in India.

Career graph:
Software Programmer since 2002.
Web Developer in ASP.NET since 2004.

I love reading the blogs and articles of technology experts. I love codeproject and stackoverflow .

I love to share knowledge and help the programmers. I appreciate if some body corrects my code or my concepts which helps me learn.

Comments and Discussions

GeneralMy vote of 4 Pin
Umesh AP3-May-16 4:00
Umesh AP3-May-16 4:00 
Simple Query to find duplicate entries.
GeneralMy vote of 1 Pin
dkboss20-Jul-12 2:02
dkboss20-Jul-12 2:02 
GeneralRe: My vote of 1 Pin
bbirajdar20-Jul-12 2:07
bbirajdar20-Jul-12 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.