InterviewPrepKit

Home / SQL / Window Functions

Deduplicate Rows with ROW_NUMBER

medium 00:00
Solving tips
  • Deduplication means one row per key: PARTITION BY the dedup key, ORDER BY your recency/tie-break rule, and keep the row where ROW_NUMBER = 1.
  • Put ROW_NUMBER in a subquery or CTE, then filter on it — you cannot reference a window alias inside WHERE in the same SELECT.
  • Make ORDER BY fully deterministic (add a unique tie-break like id) or which duplicate survives becomes arbitrary.

A contacts table has accumulated duplicate rows for the same email. You need to keep exactly one record per email (the most recently updated one) and drop the rest.

Schema

CREATE TABLE contacts (
    id         INT PRIMARY KEY,
    email      TEXT,
    full_name  TEXT,
    updated_at TIMESTAMP
);
idemailfull_nameupdated_at
1[email protected]Ann Lee2024-01-10 09:00:00
2[email protected]Bob Ray2024-01-11 10:00:00
3[email protected]Ann M Lee2024-02-01 08:30:00
4[email protected]Cara Kim2024-01-05 12:00:00
5[email protected]Robert Ray2024-03-02 14:15:00
6[email protected]Ann Lee2024-01-20 07:45:00

Task

Return one row per email: the one with the latest updated_at. Break ties on updated_at by keeping the highest id. Output columns id, email, full_name, updated_at, ordered by email ascending.

Expected output

idemailfull_nameupdated_at
3[email protected]Ann M Lee2024-02-01 08:30:00
5[email protected]Robert Ray2024-03-02 14:15:00
4[email protected]Cara Kim2024-01-05 12:00:00

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug