SQL

Delete Duplicate Rows Keeping First Occurrence

Efficiently remove duplicate entries from your database table based on specific columns in MySQL, ensuring only one unique record remains for each set of duplicates.

DELETE t1 FROM your_table t1, your_table t2
WHERE t1.id > t2.id AND t1.column1 = t2.column1 AND t1.column2 = t2.column2;
How it works: This query deletes duplicate rows from `your_table` while preserving the one with the smallest `id`. It works by self-joining the table and identifying rows that have the same values in `column1` and `column2` but a larger `id`, marking them for deletion. This is a common and efficient method for data cleansing in MySQL.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs