Click here to Skip to main content
15,868,016 members
Articles / Database Development / SQL Server
Tip/Trick

Delete Duplicate Rows in SQL Server

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

Introduction

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.

Description

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.

SQL
WITH tblTemp as
(
SELECT ROW_NUMBER() Over(PARTITION BY Name,Department ORDER BY Name)
   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.

License

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 arkadeepde@gmail.com or you can visit my blog at ASP With Arka

Comments and Discussions

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