07.03.08

Query to delete duplicate Records in SQL Server 2005

Posted in Important SQL Query, Interview Questions, SQL Server, Utlities tagged , at 11:23 am by dayananthan

Suppose we have the situation to delete some duplicate records in our table. Suppose consider one table

create table #Test
(
EmpID int,
EmpName varchar(50)
)

–Insert the Records into #Test table

insert into #Test values(1,’Daya‘)
insert into #Test values(1,’Daya‘)
insert into #Test values(1,’Daya‘)

Now i have two duplicate records inserted and i want to delete those records. The following query will delete the duplicate records

–Query to Delete Duplicate Records

WITH Emp AS (SELECT ROW_NUMBER ( ) OVER ( PARTITION BY EmpID, EmpName ORDER BY EmpID ) AS RNUM FROM #Test )
DELETE FROM Emp WHERE RNUM > 1

 

8 Comments »

  1. Vinodh said,

    If the records,then this query will help us.
    if there two different records having duplicates.Then, how to remove the duplicates of these two records.

  2. dayananthan said,

    Please provide one example as what exactly you want

  3. chandra mohan said,

    thanks your answer is right
    but that is so complex
    good luck

  4. Rakis said,

    Good One
    Thanks!!!!!!!!

  5. the query working well but need to understand i appreciate if could you explain about given query to delete the duplicate records.

    Thank you

  6. Govind said,

    WITH Emp AS (SELECT ROW_NUMBER ( ) OVER ( PARTITION BY EmpID, EmpName ORDER BY EmpID ) AS RNUM FROM #Test )
    DELETE FROM Emp WHERE RNUM > 1

    can u explain it in detail.
    i couldn’t undrestand
    Thanks

  7. webtips said,

    good one…

    I find two other method to solve the issue.

    Delete Duplicate records in SQL Server

  8. ramesh said,

    very nice post.your solution solve my problem.


Leave a Comment