SQLIntermediate#queries

How do you find and delete duplicate rows?

Group by the duplicate columns with HAVING COUNT(*) > 1 to find them, then delete using ROW_NUMBER to keep exactly one copy.

Example
WITH d AS (
  SELECT id, ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) rn
  FROM users
)
DELETE FROM users WHERE id IN (SELECT id FROM d WHERE rn > 1);

Related Questions

1
SQLIntermediate#queries

How do you find employees earning more than their department average?

Open
2
SQLIntermediate#joins#queries

How do you find records in table A that are missing in table B?

Open
3
SQLBeginner#datatypes

CHAR vs VARCHAR vs TEXT?

Open