Click here to Skip to main content
Licence CPOL
First Posted 15 Feb 2011
Views 10,909
Bookmarked 15 times

Remove Duplicate Rows from a Table in SQL Server

By SadequeSharif | 15 Feb 2011
Easiest way to delete more than one duplicate row from a table in SQL Server
 
Part of The SQL Zone sponsored by
See Also
   3.70 (7 votes)

1

2
2 votes, 28.6%
3
3 votes, 42.9%
4
2 votes, 28.6%
5
3.70/5 - 7 votes
μ 3.70, σa 1.32 [?]
 

Introduction

Most of the times, we use primary key or unique key for preventing insertion of duplicate rows in SQL Server. But if we don't use these keys, then it's obvious that duplicate rows could be entered by the user. After inserting duplicate rows into table, it becomes a major issue to delete those duplicate rows. In that time, we need to delete those duplicate rows to resolve the issue. So this topic will help us to delete those duplicate rows from the specific table.

Background

I used some basic T-SQL code to accomplish the target. So you don't need to worry to understand this code.

Problem

Firstly, we will create a table, where we will insert some duplicate rows to understand the topic properly. Create a table called ATTENDANCE by using the following code:

CREATE TABLE [dbo].[ATTENDANCE](
	[EMPLOYEE_ID] [varchar](50) NOT NULL,
	[ATTENDANCE_DATE] [date] NOT NULL
) ON [PRIMARY]   

Now insert some data into this table.

INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES
			('A001',CONVERT(DATETIME,'01-01-11',5)) 
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES
			('A001',CONVERT(DATETIME,'01-01-11',5)) 
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES
			('A002',CONVERT(DATETIME,'01-01-11',5)) 
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES
			('A002',CONVERT(DATETIME,'01-01-11',5)) 
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES
			('A002',CONVERT(DATETIME,'01-01-11',5)) 
INSERT INTO dbo.ATTENDANCE (EMPLOYEE_ID,ATTENDANCE_DATE)VALUES
			('A003',CONVERT(DATETIME,'01-01-11',5)) 

After inserting the data, check the data of the below table. If we grouped the employee_id and attendance_date, then A001 and A002 become duplicates.

EMPLOYEE_ID ATTENDANCE_DATE
A001 2011-01-01
A001 2011-01-01
A002 2011-01-01
A002 2011-01-01
A002 2011-01-01
A003 2011-01-01

So how can we delete those duplicate data?

Solution

First, insert an identity column in that table by using the following code:

ALTER TABLE dbo.ATTENDANCE ADD AUTOID INT IDENTITY(1,1)  

Now the table data will be like the following table:

EMPLOYEE_ID ATTENDANCE_DATE AUTOID
A001 2011-01-01 1
A001 2011-01-01 2
A002 2011-01-01 3
A002 2011-01-01 4
A002 2011-01-01 5
A003 2011-01-01 6

Check the AUTOID column. Now we will start playing the game with this column.

Now use the following code to find out the duplicate rows that exist in the table.

SELECT * FROM dbo.ATTENDANCE WHERE AUTOID NOT IN (SELECT MIN(AUTOID) _
	FROM dbo.ATTENDANCE GROUP BY EMPLOYEE_ID,ATTENDANCE_DATE)  

The above code will give us the following result:

EMPLOYEE_ID ATTENDANCE_DATE AUTOID
A001 2011-01-01 2
A002 2011-01-01 4
A002 2011-01-01 5

Ultimately, these are the duplicate rows which we want to delete to resolve the issue. Use the following code to resolve it.

DELETE FROM dbo.ATTENDANCE WHERE AUTOID NOT IN (SELECT MIN(AUTOID) _
	FROM dbo.ATTENDANCE GROUP BY EMPLOYEE_ID,ATTENDANCE_DATE) 

Now check the data. No duplicate rows exist in the table.

Is it too complicated?

History

  • 16th February, 2011: Initial post

License

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

About the Author

SadequeSharif

Software Developer
KDS Garment Industries limited
Bangladesh Bangladesh

Member

Follow on Twitter Follow on Twitter
Trying hard to do programming in simple way.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberMember 792008410:38 21 Jul '11  
GeneralUse the physical PinmemberMember 29568382:46 25 Feb '11  
Generalhelpfull PinmemberPranay Rana3:11 24 Feb '11  
GeneralYet another way Pinmemberfgrimm23:24 21 Feb '11  
GeneralMy vote of 3 PinmemberMoeez Shahid23:23 21 Feb '11  
QuestionOne more attempt to reinvent the wheel? PinmemberVictor Nijegorodov23:11 21 Feb '11  
AnswerRe: One more attempt to reinvent the wheel? Pinmemberfgrimm23:29 21 Feb '11  
GeneralMy vote of 4 Pinmemberdingshouqing0:38 18 Feb '11  
GeneralRe: My vote of 4 PinassociateSadequeSharif5:36 18 Feb '11  
GeneralMy vote of 4 PinmemberSlacker0071:49 16 Feb '11  
GeneralRe: My vote of 4 PinassociateSadequeSharif5:38 18 Feb '11  
GeneralOther way PinmemberRemi BOURGAREL22:43 15 Feb '11  
QuestionRe: Other way PinassociateSadequeSharif23:56 15 Feb '11  
AnswerRe: Other way PinmemberRemi BOURGAREL0:28 16 Feb '11  
GeneralRe: Other way PinassociateSadequeSharif3:08 16 Feb '11  
GeneralRe: Other way PinmemberRemi BOURGAREL3:25 16 Feb '11  
GeneralRe: Other way PinassociateSadequeSharif7:00 16 Feb '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120222.1 | Last Updated 16 Feb 2011
Article Copyright 2011 by SadequeSharif
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid