SQL Server: How To Find Duplicate Records

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

 

About Rob Rogers

Once a prolific author here on Tech-Recipes, Rob has moved on to greener pastures.
View more articles by Rob Rogers

The Conversation

Follow the reactions below and share your own thoughts.

  • Anonymous

    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.