Deleting duplicate rows from a table

Yes, assuming you have a unique ID field, you can delete all records that are the same except for the ID, but don’t have “the minimum ID” for their group of values.

Example query:

DELETE FROM Table
WHERE ID NOT IN
(
SELECT MIN(ID)
FROM Table
GROUP BY Field1, Field2, Field3, ...
)

Notes:

  • I freely chose “Table” and “ID” as representative names
  • The list of fields (“Field1, Field2, …”) should include all fields except for the ID
  • This may be a slow query depending on the number of fields and rows, however I expect it would be okay compared to alternatives

EDIT: In case you don’t have a unique index, my recommendation is to simply add an auto-incremental unique index. Mainly because it’s good design, but also because it will allow you to run the query above.

Leave a Comment