Click here to Skip to main content
15,867,915 members
Articles / Database Development / SQL Server / SQL Server 2008

How to Find Duplicate Values in a Table

Rate me:
Please Sign up or sign in to vote.
4.43/5 (6 votes)
16 Nov 2010CPOL2 min read 38.2K   8   8
This article will try to show how to find duplicate values in a table.

Duplicate values in tables can create a major problem when we do not make a primary key or a unique key on a table. In these cases, we need to find out the duplicate records and need to delete them. We can use the Having Clause to find out the duplicate records. We show this with the help of an example.

Suppose we have a table named EmployeeDeptInfo which has the column Employeid and Departmentid. The query for creating this table is given below:

SQL
Create table EmployeeDeptInfo ( Employeeid int, Departmentid int)

Employeeid contains the Id of the employee and Departmentid contains the Id of the department to which he/she belongs. This table does not have any primary key defined on it and also it doesn't have any unique key constraint defined on any column.

Suppose the table contains the following data:

EmployeeidDepartmentid
11
22
32
43
32
22
54
22

In this table, entries for the employee having employeeid 2 & 3 get repeated. In this example, the data in the table is not much for the example point of view but in real time scenario, it can be billions of rows and duplication of rows can be a very big concern. Therefore, it is necessary to find out the duplicate rows in the table. We can use the Having Clause to find out the duplicate rows.

Query for finding out the duplicate rows in the table is given below:

SQL
Select Employeeid, Departmentid from EmployeeDeptInfo Group By Employeeid, 
    DepartmentId having (count(employeeid) >1 and count (Departmentid) >1)

This query will give us the following results:

EmployeeidDepartmentid
22
32

We can also find out how many times a record is repeated in the table. For example, the following query gives us the result of how many times a given record is repeated.

SQL
Select Employeeid, Departmentid, 
    count (Employeeid) as NoOfRepeat from EmployeeDeptInfo Group By Employeeid, 
    DepartmentId having (count(employeeid) >1 and count (Departmentid) >1)

This query will give us the following results:

EmployeeidDepartmentidNoOfRepeat
223
322

Here, NoOfRepeat shows the number of times the records are duplicated in the table.

Summary

This article shows that we can find the duplicate records with the help of the Group By and Having Clause.

License

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


Written By
Database Administrator
India India
I am currently working as a Senior DBA and have around 11 years of experience in database.

Degree:-
Master Degree in Computer(MCA)

Work experience:-
Designing of the database.
Database Optimization.
Writing Complex Stored Procedures,Functions,Triggers etc.
Designing and developing SSIS & DTS packages.
Designing SQL Reports using SSRS.
Database Server Maintenance.

Certification:-
Microsoft certified Sql DBA in Sql server 2008 (MCTS).
Microsoft certified BI professional in Sql server 2008 (MCTS).
Oracle certified profession DBA in ORACLE 10g (OCP)
certified profession DBA in ORACLE 9i (OCP)

My other publication
Technical Blog:- Technologies with Vivek Johari

Moderator and Blogger at BeyondRelational.com

Guest Author and Blogger at sqlservercentral.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Oshtri Deka5-May-11 1:42
professionalOshtri Deka5-May-11 1:42 
GeneralRe: My vote of 5 Pin
Vivek Johari3-Nov-11 1:29
Vivek Johari3-Nov-11 1:29 
GeneralMy vote of 3 Pin
BloodyBaron30-Jan-11 8:25
BloodyBaron30-Jan-11 8:25 
GeneralRe: My vote of 3 Pin
Vivek Johari3-Nov-11 1:29
Vivek Johari3-Nov-11 1:29 
GeneralMy vote of 3 Pin
Dilip Baboo22-Nov-10 6:32
Dilip Baboo22-Nov-10 6:32 
How to delete them would be great too !
GeneralRe: My vote of 3 Pin
Flamewave422-Nov-10 8:43
Flamewave422-Nov-10 8:43 
GeneralExcellent Tip Pin
Flamewave422-Nov-10 5:22
Flamewave422-Nov-10 5:22 
GeneralRe: Excellent Tip Pin
Vivek Johari22-Nov-10 8:04
Vivek Johari22-Nov-10 8:04 

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.