How to remove duplicate records in mysql

In this article, We’ll see how to remove duplicate records in mysql




We’ve created a users table i.e tbl_users and added some records inside table.

users table

Delete duplicate records using Sub Query

DELETE FROM `tbl_users` WHERE `id` NOT IN (SELECT `id` FROM (SELECT MAX(id) AS id FROM `tbl_users` GROUP BY `name`) AS tbl);

or

We can use below sql query.

DELETE u1 FROM tbl_users u1 INNER JOIN tbl_users u2 WHERE u1.id > u2.id AND u1.name = u2.name;

After executed the query, we’ve removed the duplicate records from users table.

users table

That’s it!. Please share your thoughts or suggestions in the comments below.

Leave a Reply

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