SQL

Delete Duplicate Rows While Keeping the Earliest/Lowest ID

Clean up your database by efficiently removing redundant entries based on specific columns, retaining only the record with the minimum ID for each duplicate set.

DELETE FROM your_table
WHERE id NOT IN (
    SELECT MIN(id)
    FROM your_table
    GROUP BY column1, column2, column3 -- Specify columns that define a duplicate
);
How it works: This query deletes duplicate rows from `your_table` while ensuring one unique record is kept for each set of duplicates. It works by identifying the minimum `id` for every unique combination of `column1`, `column2`, and `column3` (the columns that define a duplicate). All rows whose `id` is not among these minimum IDs are then deleted.

Need help integrating this into your project?

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

Hire DigitalCodeLabs