SQL
Deleting Duplicate Records While Retaining One
Learn how to identify and remove duplicate rows in your SQL tables, ensuring data integrity by keeping a single, unique instance of each record based on specific columns.
DELETE FROM your_table
WHERE id NOT IN (
SELECT MIN(id)
FROM your_table
GROUP BY column1, column2 -- Specify columns that define a duplicate
);
How it works: This SQL snippet efficiently removes duplicate records from a table while preserving one unique instance. It works by first identifying the minimum `id` for each group of rows that share the same values in `column1` and `column2` (which define what makes a record a duplicate). Then, it deletes all rows whose `id` is *not* among these minimum IDs, effectively keeping only the earliest (or lowest ID) instance of each unique combination.