Find and Delete Duplicate Records From SQL Table


This article helps you to identify duplicate records in a SQL table and removes the records from that table.

Now first I create a table

TABLE duptext (id varchar(50),name varchar(50),salary int);

The preceding query returns the query result shown in the following image:


Description of duptext table


Insert values into duptext table

You can simply use the following query to insert multiple rows in a single SQL query.

into duptext VALUES (1,'sharad',100),(1,'sharad',100),(1,'sharad',100),(2,'nitin',200),(3,'vinod',300),(4,'rahul',400)

Data of duptext table


Count duplicate records query

Before writing the query to count the total duplicate records in a table, I will first describe how to manage this type of query, so you require the "group by" and "having" clauses and the "count" function and a "select" statement for retrieving the total number of duplicate records.

The following specifies which clause and which function corresponds to the action to be performed:

  • Group by clause: determines how the selected rows are grouped.

  • Having clause: determines which groups are included in the final result.

  • Count Function: returns the total number of rows in a table.

So, in the following query we use the "id" column with the group by clause that determines that the id column is to be grouped, the count function only counts records on the basis of the groped id and the having clause is included at the final result so it returns the id column's count value whose id exists more than once.

count(*) as DuplicateRecords from duptext group by id having count(*)>1



Now you have determined the total number of duplicate records. Now let's move on to the deletion of duplicate records. There are many ways to delete duplicate records from a SQL table and I describe one of them here.

To delete duplicate records from a table, use the following procedure.

Step 1

Create a temporary table and copy all the distinct rows into it from the "duptext" table, like:

Distinct * INTO temptable from duptext

Step 2

Delete all records of the "duptext" table.


Step 3

Now copy all data from the "temptable" into the "duptext" table, as in the following:

INTO duptext select * from  temptable

Step 4

Drop temptable

table temptable

Now you have successfully counted and deleted duplicate records from the SQL table.

Quick View Of all things