SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Deleting Duplicate Records

By Amitesh Kumar,

Deleting duplicate records:


Self join the table on all the columns that can possibly have same values and differentiate b/w the duplicate values based on the identity column. In the provided script I've taken Employees table which has some duplicate rows. The duplicacy is possible on EmployeeNo and EmployeeId. Id is a Identity column. TO find and delete the duplicate records, I join the Employee table with itself on the non-unique columns, EmployeeNo and EmployeeId in this case.


The table in consideration must have at least one non-unique column. If a table does not have any identity column then there are two possiblities :

i. Use the get row_num sql-query in conjunction with this script in place of unique field.


* Create a copy of the table using:

Select * into EmployeesCopy from Employees where 1 = 0

* Alter table to add an identity column in it.

* Insert all records from Employees to EmployeesCopy.

* Delete duplicates using sbove script implementation

* Delete all records from Employees

* Copy the new contents from EmployeesCopy to Employees

Total article views: 2791 | Views in the last 30 days: 1
Related Articles

Delete Duplicates from table with no identity column

delete duplicates from table with all columns with datatype varchar()


delete duplicates

delete duplicates


identity column

identity column


Delete Duplicate Records,

How can i delete duplicate records without changing their Identity values.


Identity column

Is identity column a good idea?

duplicate records