How To Delete Duplicate Rows in MySQL
Data duplication happens because of many reasons. Removing duplicate rows is one of the important tasks that you must deal with when working with the databases.
In this tutorial, you will learn various ways to delete duplicate rows in MySQL.
Delete duplicate rows using DELETE JOIN statement
The following statement deletes duplicate rows and keeps the highest id:
You can execute the query that find duplicate emails again to verify the delete:
The query returns an empty set, which means that the duplicate rows have been deleted.
In case you want to delete duplicate rows and keep the lowest id, you can use the following statement:
Delete duplicate rows using an intermediate table
The following shows the steps for removing duplicate rows using an intermediate table:
- Create a new table whose structure is the same as the original table:
- Insert distinct rows from the original table to the new table:
- Drop the original table and rename the immediate table to the original one:
For example, the following statements delete rows with duplicate emails from the contacts table:
Delete duplicate rows using the ROW_NUMBER() function
The following statement uses the ROW_NUMBER() function to assign a sequential integer number to each row. If the email is duplicate, the row number will be greater than one.
The following statement returns id list of the duplicate rows:
And you just delete the duplicate rows from the contacts table using the DELETE statement with a subquery in the WHERE clause:
In this tutorial, you have learned how to delete duplicate rows in MySQL.