Finding Duplicates With SQL

I’ve been asked several times over the last few days to help people find duplicates within a particular table, and below is a very basic bit of SQL which will do exactly that:

SELECT Column1,
       COUNT(Column1) [Data Count]
 
FROM Table
 
GROUP BY Column1
 
HAVING COUNT(Column1) > 1

Fairly straight forward, every where that I’ve written “Column1”, put the name of the column that you’d like to check for duplicates. You’ll obviously also need to replace “table” with the name of your table.

The “HAVING” clause just makes sure that only records with more than 1 record are returned, this can be modified as you please, or removed completely.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>