SQL Server: How To Find Duplicate Records

Contributor Icon Contributed by Rob Rogers  
Tag Icon Tagged: Database  

When checking the integrity of your data, it may be necessary to check your tables for duplicate records. By grouping these records, you can eliminate the unique records from your result set so that you can view just the records that contain duplicated values, making it easier to remove them.

In our example, we will work with a table named Students and we will look for duplicate values in the Email_Address column.

By using the following SELECT statement, you can isolate the records with duplicate values:

SELECT Email_Address, COUNT(*) FROM Students
Group BY Email_Address HAVING COUNT(*) > 1

You can use the opposite of this to find all rows with unique values in the Email_Address column by using the following SELECT statement:

SELECT Email_Address, COUNT(*) From Students
GROUP BY Email_Address HAVING COUNT(*) = 1

 

1 Comment -


  1. Anonymous said on March 12, 2010

    It gets more complicated if you would like to compare the row on more than one column. In that case you can use the ranking functions which come with SQL Server 2005 / 2008.

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -