SQL
Delete Duplicate Rows Keeping One Instance
Clean up database tables by identifying and deleting duplicate rows based on specific columns, while preserving a single, unique instance of each record.
DELETE FROM your_table
WHERE id NOT IN (
SELECT MIN(id)
FROM your_table
GROUP BY column1, column2
);
How it works: This powerful SQL snippet allows you to delete duplicate rows from a table, ensuring data integrity by keeping only one instance of each unique record. It works by using a subquery to find the minimum `id` for each group of duplicate `column1` and `column2` values. The outer `DELETE` statement then removes all rows whose `id` is not among these minimum IDs, effectively removing all but one duplicate.