How to delete duplicate records from a table in sql

Smith
Smith
None
2568 Points
74 Posts

I have Emp table with duplicate record. I want a single command to remove duplicate record that is, keep one record from the duplicates.

Views: 8675
Total Answered: 2
Total Marked As Answer: 1
Posted On: 23-Nov-2015 23:03

Share:   fb twitter linkedin
Answers
NiceOne Team
NiceOne...
Editor
1382 Points
14 Posts
         

hi smith,

You can use ROW_NUMBER() with PARTITION BY to delete duplicate.

Suppose you have emp table with duplicate records as:

Name Salary
abc 20000.00
abc 20000.00
xyz 10000.00

and want emp after deletion of duplicate records as:

Name salary
abc 20000.00
xyz 10000.00

Use following CTE Query to delete duplicate record from the table emp:

WITH tmp AS(SELECT name,salary,ROW_NUMBER() OVER(PARTITION BY name,salary ORDER BY name) AS duplicateCount FROM emp)
DELETE FROM tmp WHERE tmp.duplicateCount>1

Now execute select query again

SELECT Name, salary
FROM Emp

result will be as:

Name salary
abc 20000.00
xyz 10000.00
Posted On: 20-Dec-2015 00:11
Smith
Smith
None
2568 Points
74 Posts
         

Thanks...

Finally I got the solution.

Posted On: 26-Dec-2015 01:06
 Log In to Chat