Click here to Skip to main content
15,888,454 members
Articles / Database Development / SQL Server

Remove Duplicate Rows from a Table in SQL Server

Rate me:
Please Sign up or sign in to vote.
4.84/5 (49 votes)
15 Feb 2011CPOL2 min read 610.8K   52   50
Easiest way to delete more than one duplicate row from a table in SQL Server

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:

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

Now insert some data into this table.

SQL
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_IDATTENDANCE_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:

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

Now the table data will be like the following table:

EMPLOYEE_IDATTENDANCE_DATEAUTOID
A0012011-01-011
A0012011-01-012
A0022011-01-013
A0022011-01-014
A0022011-01-015
A0032011-01-016

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.

SQL
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_IDATTENDANCE_DATEAUTOID
A0012011-01-012
A0022011-01-014
A0022011-01-015

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

SQL
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)


Written By
Founder Codexplorer Technologies
Bangladesh Bangladesh
I am:
Founder & Technical Head at Codexplorer Technologies.
IT Consultant at Meridian Group.

I was:
Manager (IT) at Meridian Group.
Assistant Manager (Software Division) at KDS Garment Industries Limited.
Assistant Manager (Software Division) at E-Vision Software Limited.

My blog:
crea8ivecode

My preferred work area:
ASP.NET & SQL SERVER.

My email:
sadeque.sharif@yahoo.com

Follow me:
twitter | facebook | linkedin

Comments and Discussions

 
GeneralRe: My vote of 4 Pin
Sadeque Sharif18-Feb-11 4:36
professionalSadeque Sharif18-Feb-11 4:36 
GeneralMy vote of 4 Pin
Slacker00716-Feb-11 0:49
professionalSlacker00716-Feb-11 0:49 
GeneralRe: My vote of 4 Pin
Sadeque Sharif18-Feb-11 4:38
professionalSadeque Sharif18-Feb-11 4:38 
GeneralOther way Pin
Remi BOURGAREL15-Feb-11 21:43
Remi BOURGAREL15-Feb-11 21:43 
QuestionRe: Other way Pin
Sadeque Sharif15-Feb-11 22:56
professionalSadeque Sharif15-Feb-11 22:56 
AnswerRe: Other way Pin
Remi BOURGAREL15-Feb-11 23:28
Remi BOURGAREL15-Feb-11 23:28 
GeneralRe: Other way Pin
Sadeque Sharif16-Feb-11 2:08
professionalSadeque Sharif16-Feb-11 2:08 
GeneralRe: Other way Pin
Remi BOURGAREL16-Feb-11 2:25
Remi BOURGAREL16-Feb-11 2:25 
it should be working, maybe a little syntax error...
I couldn't try it because your comment's table is not the same as your article's.

Why isn't it working ? What is the result and what is the result expected ?

It's fine if you don't want to improve your solution, just trying to help you here : your solution delete randomly some rows in a table only because they share 2 values, I don't see where this can be relevant.
GeneralRe: Other way Pin
Sadeque Sharif16-Feb-11 6:00
professionalSadeque Sharif16-Feb-11 6:00 

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.