5 Quick Ways To Delete Duplicate Records From Database Which Every Developer Must Know

When you are working with Database-Driven applications, you should definitely know how to remove duplicated records from your database table. Duplicate records not only take a massive amount of space but also make the database query execution process slower. Therefore, every developer should know how to delete duplicate records.

I have decided to write this tutorial that tells 5 quick ways to delete duplicate records from a Database table. I hope you will enjoy reading and learning from it.

The Database Table which contains the duplicate records

I have a table called ‘Student’ that contains some duplicate records which need to be deleted. This table looks like:

It has 4 columns which are, 
  1. Id – a primary column of type ‘int’ and Identity[1,1]
  2. Name – varchar(50) and NOT NULL
  3. Age – int and NOT NULL
  4. Standard – int and NOT NULL

You can create this table by running the following script in your SQL Server Database.

  2. GO    
  5. GO    
  8. GO    
  10. CREATE TABLE [dbo].[Student](    
  11.     [Id] [int] IDENTITY(1,1) NOT NULL,    
  12.     [Name] [varchar](50) NOT NULL,    
  13.     [Age] [intNOT NULL,    
  14.     [Standard] [intNOT NULL,    
  16. (    
  17.     [Id] ASC    
  19. ON [PRIMARY]    
  21. GO    
  24. GO    

Now I add some duplicate records to this table by running the following insert scripts,

  1. SET IDENTITY_INSERT [dbo].[Student] ON     
  3. GO    
  4. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (1, N'John', 12, 5)    
  5. GO    
  6. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (2, N'Ram', 11, 4)    
  7. GO    
  8. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (3, N'Ram', 11, 4)    
  9. GO    
  10. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (4, N'Anna', 8, 3)    
  11. GO    
  12. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (5, N'Maya', 9, 4)    
  13. GO    
  14. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (6, N'Mary', 10, 5)    
  15. GO    
  16. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (7, N'Mary', 10, 5)    
  17. GO    
  18. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (8, N'Mary', 10, 6)    
  19. GO    
  20. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (9, N'Robin', 11, 5)    
  21. GO    
  22. INSERT [dbo].[Student] ([Id], [Name], [Age], [Standard]) VALUES (10, N'Donald', 12, 7)    
  23. GO    
  24. SET IDENTITY_INSERT [dbo].[Student] OFF    
  25. GO   
Now open the table's data and see the duplicate records as shown by the below image:

The duplicate records are the students with the below names:

  • Ram with id 2 and 3.
  • Mary with id 6, 7

Note that the student with name Mary and Id 5 is not a duplicate of other Mary records (id 6 and 7) because her standard is different than other Mary records.

My Database table is ready, now let us start with 5 ways to delete duplicate records from this table: 

Way 1: Using Group By

SQL Group By statement groups the results by one or more columns and is used by with aggregate functions (COUNT, MAX, MIN, SUM, AVG).

So I create an SQL Query using Group By and Min statements which will return ‘Non-Duplicate’ records. This query is,

  1. SELECT MIN(Id) AS 'Id'Name, Age, Standard     
  2. FROM Student     
  3. GROUP BY Name, Age, Standard    

You can see in the above image that the students with id 3 and 7 are missing since they are the duplicate records.

Now, I can use the Delete statement in my query to delete these records. The query is,

  2. Id NOT IN(SELECT MIN(Id) AS 'Id' FROM Student GROUP BY Name, Age, Standard)    

After running this query, the table duplicate records get deleted as shown in the below image.


Way 2: Using Distinct Statement

If you want to use Distinct Statement which returns Non-Duplicate rows then you will have to create a new table with an exact definition like your previous table. Then insert all ‘Non-Duplicate’ rows from the previous table to the new table.

I created a new table called Student1 and added all the distinct records to it from my previous table called ‘Student’ using the below query:

  1. Select  Distinct * into Student1 from Student;    

Now I can drop my ‘Student’ table and rename ‘Student1’ to ‘Student’. The below 2 queries do this work for me:

  1. DROP Table Student  
  2. EXEC sp_rename 'Student1''Student'  

Way 3: Using Programming Language

You can also delete the duplicate records with the help of some programming language like C#, JavaScript, PHP, etc.

Let me take the help of Entity Framework Core which is an OR/M(Object-Relational Mapping) framework written in C# language. Entity Framework Core is widely used in ASP.NET Core database programming.

In Entity Framework Core, the code to delete the duplicate records is given below:

  1. using (var context = new MyContext())    
  2. {    
  3.     var students = context.Student.GroupBy(s => new { s.Name, s.Age, s.Standard }).SelectMany(grp => grp.Skip(1)); ;    
  4.     context.Student.RemoveRange(students);    
  5.     context.SaveChanges();    
  6. }    

I have used GroupBy() function of Entity Framework Core to find out all the duplicate records based on 3 columns - 'Name, Age, Standard'.

Next I use the RemoveRange() method to delete these duplicate records.

Way4: Using Common Table Expressions (CTE)

Common table expression (CTE) is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. I use CTE to with ‘PARTITION BY’ statement to delete duplicate records like this:

  1. WITH myTemp    
  2. AS (SELECT ROW_NUMBER() OVER (PARTITION BY Name, Age, Standard ORDER BY Id) RN FROM Student)    
  3. DELETE FROM myTemp WHERE RN > 1;   
Notice the Row_Number() function which provides consecutive numbering of the rows in the result by the order selected in the OVER clause. It will assign the value 1 for the first row and increase the number for the subsequent rows.
I have put the 3 columns of the Student’s table in the Partition By statement. When you specify a column or set of columns with Partition By statement then it will divide the result set into record partitions and then finally ranking functions are applied to each record partition separately and the rank will restart from 1 for each record partition separately. 

Way5: Using Self Join

Self Join which is an Inner Join on the same table can also be used to remove duplicate records. Here you create the self-join by making use of 2 things which are given below,

  • In the ‘on’ condition you check the duplicate columns.
  • In the ‘where’ condition you compare the ‘primary key’ columns of the tables.

So, the below Self Join query will give me the duplicate columns of the ‘Student’ table:

  1. Select * From Student s Inner Join Student s1     
  2. On s.Name=s1.Name And s.Age=s1.Age And s.Standard=s1.Standard     
  3. Where s.Id>s1.Id    

So all I have to do is just put a Delete statement in order to delete the duplicate records,

  1. Delete Student where Id in(    
  2. Select s.Id From Student s Inner Join Student s1     
  3. On s.Name=s1.Name And s.Age=s1.Age And s.Standard=s1.Standard     
  4. Where s.Id>s1.Id)    


In this tutorial, you learned 5 quick ways to delete your duplicate records from a database table. You can choose any deletion method based on your liking. Don’t forget to like this tutorial and share it with your friends.

Do read my other related database tutorial on C# Corner which is How To Restore Northwind Database In SQL Server In One Minute