SQL

Delete Duplicate Rows While Keeping One Unique Record

Clean up your database by removing duplicate rows, ensuring data integrity by retaining a single unique record based on specified columns.

DELETE FROM your_table_name
WHERE unique_id NOT IN (
  SELECT MIN(unique_id)
  FROM your_table_name
  GROUP BY col1, col2 -- Columns defining uniqueness
);
How it works: This query deletes duplicate rows from a table while preserving one unique instance. It identifies the minimum 'unique_id' for each set of duplicate rows (defined by 'col1' and 'col2') and then removes all other rows whose 'unique_id' is not among these minimums. Replace 'unique_id' with your table's primary key or unique identifier column.

Need help integrating this into your project?

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

Hire DigitalCodeLabs