Click here to Skip to main content
13,861,926 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

75.7K views
3 downloads
27 bookmarked
Posted 26 Oct 2014
Licenced CPOL

Delete Duplicate Rows in SQL Server

, 26 Oct 2014
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Arkadeep De
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

You may also be interested in...

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
Gheorghe Musca28-Oct-14 22:15
memberGheorghe Musca28-Oct-14 22:15 
GeneralRe: UNIQUE constraint? Pin
Arkadeep De29-Oct-14 0:32
professionalArkadeep De29-Oct-14 0:32 
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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190214.1 | Last Updated 27 Oct 2014
Article Copyright 2014 by Arkadeep De
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid