Delete or remove duplicate rows in MySQL

You can easily remove duplicate rows in MySQL and keep just one record for each. I have provided two solutions here, one is simple query and second is using subquery to attain the same.

1. Using Query

This will remove all the duplicate entries in your MySQL database while keeping row with the lowest id value.

If you wish to delete duplicate rows except row with highest id value, you can alter the query in following manner:

2. Using Subquery

Keep the row with the lowest id value and remove duplicate rows:

Keep the row with the highest id value and delete duplicate rows:

The subquery in a subquery is necessary for MySQL, or you’ll get a 1093 error.

While performing any of these queries, you should do this first on a test copy of your table.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.