Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
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: 2774 | Views in the last 30 days: 2
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    

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones