How do you remove duplicate rows, keeping the latest version of each key?
Scenario
A customer feed lands every hour and the same customer arrives several times with updated details. Silver must hold one row per customer: the latest.
Interview answerTry answering first, then reveal
Number the rows per business key with ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY updated_at DESC, ingested_at DESC) and keep rn = 1, using a CTE or QUALIFY. Write the result to a new table or MERGE it into the target; deleting "from the CTE" only works in SQL Server.
Explanation
ROW_NUMBER (not RANK) guarantees exactly one row per key, even if two versions share the same updated_at. The second sort column decides those ties deterministically.
A plain DISTINCT does not help: the versions differ in some column, so they are not duplicates to DISTINCT. Those are versions of a key, and you have to choose one.
How you apply it depends on the engine. In a lakehouse you usually rebuild the clean table or MERGE the latest rows into it. In PostgreSQL you can delete with a join on a unique row id; in SQL Server you can delete through a CTE.
Worked example
Run on DuckDB when this page was built; the results shown are the real output.
Sample data
CREATE TABLE customers_raw (customer_id INT, email VARCHAR, city VARCHAR,
updated_at TIMESTAMP, ingested_at TIMESTAMP);
INSERT INTO customers_raw VALUES
(1, 'asha@x.com', 'Pune', '2026-02-01 10:00', '2026-02-01 11:00'),
(1, 'asha@x.com', 'Mumbai', '2026-02-05 09:00', '2026-02-05 10:00'),
(2, 'ben@x.com', 'Delhi', '2026-02-03 12:00', '2026-02-03 13:00'),
(2, 'ben@new.com', 'Delhi', '2026-02-03 12:00', '2026-02-03 14:00'),
(3, 'chen@x.com', 'Chennai','2026-02-02 08:00', '2026-02-02 09:00');Latest version per customer
CREATE OR REPLACE TABLE customers AS
SELECT customer_id, email, city, updated_at
FROM customers_raw
QUALIFY ROW_NUMBER() OVER (PARTITION BY customer_id
ORDER BY updated_at DESC, ingested_at DESC) = 1;
SELECT * FROM customers ORDER BY customer_id;| customer_id | city | updated_at | |
|---|---|---|---|
| 1 | asha@x.com | Mumbai | 2026-02-05 09:00 |
| 2 | ben@new.com | Delhi | 2026-02-03 12:00 |
| 3 | chen@x.com | Chennai | 2026-02-02 08:00 |
Customer 2 has two versions with the same updated_at; the later ingested_at wins, so the new email is kept.
In production
- Agree the ordering rule with the source owner. "Latest" can mean source update time, CDC sequence number or ingestion time, and they disagree when data arrives late.
- In Delta Lake or Iceberg, deduplicate the incoming batch this way and then
MERGEinto the target with a condition such asWHEN MATCHED AND s.updated_at > t.updated_at THEN UPDATE, so an older late record never overwrites a newer one. - Count rows before and after, and alert if the duplicate ratio jumps: it often signals a broken upstream extract.
Common mistakes
WITH d AS (...) DELETE FROM d WHERE rn > 1outside SQL Server: PostgreSQL, Spark SQL and most others reject deleting from a CTE.- Using
RANK, which keeps both rows when the timestamps tie. - Using
MAX(updated_at)withGROUP BYand then joining back, which also returns two rows on ties and is slower.
Related questions
More on this topic in the SQL Interview Questions guide, or browse all SQL questions.