Introduction
Here I will explain how to Delete Duplicate Record or Rows from Table in SQL Server. I am not going in detail or background of the article its a common problem which occurs time to time with developers so here i just explain how solve your problem.
SQL query to delete duplicate rows
create a table like this,
create table Emp(empid int,name varchar(20))
Table Emp
| empid | name |
| 1 | abc |
| 1 | def |
| 2 | abc |
| 2 | abc |
Enter some random or duplicate value in table:
Method 1
- select distinct * into #tmptbl From Emp
- delete from Emp
- insert into Emp
- select * from #tmptbl drop table #tmptbl
Method 2
You can do with CTE (Common Table Expression).
- WITH cte AS (
- SELECT empid , name ,
- row_number() OVER(PARTITION BY empid , name order by empid ) AS [rn]
- FROM dbo.Emp
- )
- DELETE cte WHERE [rn] > 1
Method 3
- delete from Emp where empid in(select empid from Emp group by empid having count(*) >1)

Shubham KumarPosted Apr 12, 2015, 7:26 AM
thnx for the comment and @pankaj i appreciate your suggestion thnx
NitinPosted Mar 10, 2015, 3:32 AM
good one
Pankaj Kumar ChoudharyPosted Feb 24, 2015, 8:41 PM
you can also use this method delete T1 from MyTable T1, MyTable T2 where T1.dupField = T2.dupFieldand T1.uniqueField > T2.uniqueField
Khargesh RajputPosted Feb 10, 2015, 4:48 AM
helpful tips
Srinivasan K KPosted Feb 9, 2015, 1:36 AM
@ Shubham, Quick tip. Very helpful.