How to delete duplicate records from MySQL table | MySQL query to delete duplicate records

Many of the interviews I was asked a common MySQL question and the questions was, Write a MySQL query to delete the duplicated records from MySQL table.
Here in this small article I'll be explaining MySQL query to get duplicate records and to delete the duplicate records.

For example you have a users table and inside the users table we have duplicate email id of user so the first question how to get the duplicate records ?
Here is the MySQL query to get the duplicate records:

It will return duplicate emails along with the count of duplicate emails.



Now we wanted to delete the duplicate records. Here are some scenarios, either you wanted to keep the highest record or you wanted to keep the lowest record.

The following MySQL query will delete the duplicate records and keeps the highest id:




The following MySQL query will delete the duplicate records and keeps the lowest id:

 

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *