Click here to Skip to main content
15,499,046 members
Articles / Database Development / SQL Server
Posted 26 Oct 2014

Tagged as


27 bookmarked

Delete Duplicate Rows in SQL Server

Rate me:
Please Sign up or sign in to vote.
4.65/5 (22 votes)
26 Oct 2014CPOL
How to remove duplicate records using CTE in SQL


Sometimes, some duplicate data may be inserted in the SQL table. But it's harmful for you and your application too. So how do you remove the duplicate rows in your table.


Take a look at the following table content. It has duplicate rows.

Id Name Department
1 Arka .NET
2 Ashok .NET
3 Anubhav php
4 Arka .NET
5 Sucheta Graphics

Row 1 and 4 are duplicates. So we have to remove one row and keep one in the table.

Now the question is how to do this?

Using the Code

To solve this problem, take a look at the following code. This will solve this problem.

WITH tblTemp as
   As RowNumber,* FROM <table_name>
DELETE FROM tblTemp where RowNumber >1

After running, this code shows the table data, it will ensure that all unique rows are present.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer PwC
India India
I am a Software developer having an experience of 5 years in application development. To get me you can mail me at or you can visit my blog at ASP With Arka

Comments and Discussions

PraiseSaved my day Pin
Member 1411619213-Jan-19 7:15
MemberMember 1411619213-Jan-19 7:15 
Praiseit's cool Pin
Ronald Luna Ramos30-Jan-18 3:35
MemberRonald Luna Ramos30-Jan-18 3:35 
PraiseThank you, Thank you Pin
Ntingab5-May-16 5:04
MemberNtingab5-May-16 5:04 
QuestionTHANKS Pin
Member 871442124-Jul-15 11:24
MemberMember 871442124-Jul-15 11:24 
QuestionAnother simple solution Pin
govardhan4u29-Oct-14 7:18
Membergovardhan4u29-Oct-14 7:18 
AnswerRe: Another simple solution Pin
Arkadeep De2-Nov-14 7:48
professionalArkadeep De2-Nov-14 7:48 
AnswerRe: Another simple solution Pin
aarif moh shaikh8-Mar-15 21:25
professionalaarif moh shaikh8-Mar-15 21:25 
QuestionDistinct keyword Pin
Lorenzo A.28-Oct-14 23:17
MemberLorenzo A.28-Oct-14 23:17 
AnswerRe: Distinct keyword Pin
Arkadeep De29-Oct-14 0:28
professionalArkadeep De29-Oct-14 0:28 
GeneralUNIQUE constraint? Pin
Fly Gheorghe28-Oct-14 22:15
MemberFly Gheorghe28-Oct-14 22:15 
GeneralRe: UNIQUE constraint? Pin
Arkadeep De29-Oct-14 0:32
professionalArkadeep De29-Oct-14 0:32 
GeneralRe: UNIQUE constraint? Pin
Fly Gheorghe25-Jan-22 9:05
MemberFly Gheorghe25-Jan-22 9:05 
GeneralMy vote of 5 Pin
dmjm-h28-Oct-14 12:20
Memberdmjm-h28-Oct-14 12:20 
GeneralRe: My vote of 5 Pin
Arkadeep De29-Oct-14 0:31
professionalArkadeep De29-Oct-14 0:31 
GeneralMy vote of 5 Pin
E. Scott McFadden28-Oct-14 9:33
professionalE. Scott McFadden28-Oct-14 9:33 
GeneralRe: My vote of 5 Pin
Arkadeep De29-Oct-14 0:30
professionalArkadeep De29-Oct-14 0:30 
QuestionVery nice. Pin
Member 1017996028-Oct-14 8:42
MemberMember 1017996028-Oct-14 8:42 
AnswerRe: Very nice. Pin
Arkadeep De29-Oct-14 0:30
professionalArkadeep De29-Oct-14 0:30 
QuestionMy Vote of 5 Pin
Rajesh waran28-Oct-14 0:50
professionalRajesh waran28-Oct-14 0:50 
AnswerRe: My Vote of 5 Pin
Arkadeep De28-Oct-14 1:55
professionalArkadeep De28-Oct-14 1:55 
GeneralRe: My Vote of 5 Pin
Rajesh waran28-Oct-14 2:15
professionalRajesh waran28-Oct-14 2:15 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun27-Oct-14 2:57
MemberHumayun Kabir Mamun27-Oct-14 2:57 
GeneralRe: My vote of 5 Pin
Arkadeep De27-Oct-14 3:20
professionalArkadeep De27-Oct-14 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.