Delete duplicates keeping one row
Rank rows per duplicate key with ROW_NUMBER, preview the losers, then delete everything ranked after first. Run the DELETE inside a transaction so you can roll it back.
The task
customers contains repeated email addresses and you want to keep one row per email (the earliest by id) and remove the rest without touching the survivors.
The snippet
Runs unchanged on current versions of SQL Server, PostgreSQL, MySQL, SQLite and Oracle.
-- Step 1: preview exactly which rows will be deleted
SELECT id, email, rn
FROM (
SELECT id, email,
ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) AS rn
FROM customers
WHERE email IS NOT NULL
) ranked
WHERE rn > 1;
-- Step 2: open a transaction first (see below), then delete
DELETE FROM customers
WHERE id IN (
SELECT id
FROM (
SELECT id,
ROW_NUMBER() OVER (PARTITION BY email ORDER BY id) AS rn
FROM customers
WHERE email IS NOT NULL
) ranked
WHERE rn > 1
);
-- check the row count, then COMMIT or ROLLBACK
工作原理
ROW_NUMBER numbers rows within each email group, ordered by id, so rn = 1 is the keeper and every rn > 1 row is a redundant copy. The WHERE email IS NOT NULL matters: without it every customer with no email lands in one partition and all but one of them get deleted. The ranking sits in a derived table rather than a CTE because Oracle does not accept WITH in front of DELETE, and MySQL refuses a subquery that reads the DELETE's own table unless it is materialized like this (error 1093). The statement itself is portable; the transaction syntax is not: BEGIN on PostgreSQL and SQLite, START TRANSACTION on MySQL, BEGIN TRANSACTION on SQL Server, and nothing on Oracle, which opens a transaction implicitly. Also check what your collation calls equal: MySQL and SQL Server compare case-insensitively by default, so [email protected] and [email protected] are one email there.
Variations
Keep the most recent row instead
DELETE FROM customers
WHERE id IN (
SELECT id
FROM (
SELECT id,
ROW_NUMBER() OVER (
PARTITION BY email
ORDER BY updated_at DESC, id DESC
) AS rn
FROM customers
WHERE email IS NOT NULL
) ranked
WHERE rn > 1
);
Without window functions (MIN per group)
DELETE FROM customers
WHERE email IS NOT NULL
AND id NOT IN (
SELECT keep_id
FROM (
SELECT MIN(id) AS keep_id
FROM customers
WHERE email IS NOT NULL
GROUP BY email
) keepers
);
Questions that come up
Why the transaction if the preview already matched?
Other sessions can insert or change rows between your preview and the DELETE, and a transaction does not stop that. What it gives you is an undo: after the DELETE, re-run the preview (it should return nothing) and compare the deleted-row count with what the preview showed. If either looks wrong, ROLLBACK restores the table; COMMIT only when both match.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.