Learn SQL
-- filtering.dedup_keep_one

Dedup: Keep One Row per Group in SQL

GROUP BY, DISTINCT ON, or ROW_NUMBER — DISTINCT alone cannot choose

--concept

DISTINCT collapses whole rows but cannot pick which duplicate survives. Choices: GROUP BY with MIN/MAX per group; PostgreSQL DISTINCT ON (first row per group under ORDER BY); portable ROW_NUMBER() partitioned by key ordered by recency with outer rn = 1 (keeps latest per user deterministically with id tiebreak).

--how it works
1

DISTINCT cannot choose

Collapses whole rows; survivor is undefined for per-group picks.

2

GROUP BY picks via agg

MIN/MAX per key — earliest/latest scalar per group.

3

DISTINCT ON shortcuts (PG)

First row per group under ORDER BY — concise, non-portable.

4

ROW_NUMBER keeps latest

PARTITION BY key ORDER BY created_at DESC, id DESC; filter rn=1 — portable + deterministic.

--visualization
ROW_NUMBER partition-filter: top earner per dept (DISTINCT cannot attach names)
namedepartmentsalary
CynthiaEngineering91000
ElenaSales58000
DavidMarketing47000
Stage 1 / 1
PARTITION slices, ORDER ranks, rn=1 keeps head. DISTINCT ON (PG) is the concise twin.
--example
query.sql
1SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn FROM employees) t WHERE rn = 1;
Result
namedepartmentsalary
CynthiaEngineering91000
ElenaSales58000
DavidMarketing47000

Partition slices per department, ORDER ranks inside, outer filter keeps each head. DISTINCT could list departments but never attach the top earner’s name.

--variants

Top earner per department

Portable keep-one with names attached.

query.sql
1SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC, id DESC) AS rn FROM employees) t WHERE rn = 1;
Result
namedepartmentsalary
CynthiaEngineering91000
ElenaSales58000
DavidMarketing47000
1Partition slices per team; ORDER ranks inside.
2id tiebreak makes ties deterministic.
3Outer filter keeps each head — latest-per-user generalizes.

Earliest id per department

GROUP BY fallback without windows.

query.sql
1SELECT department, MIN(id) AS first_id FROM employees GROUP BY department;
Result
departmentfirst_id
Engineering1
Marketing4
Sales2
1Buckets per department.
2MIN picks earliest id scalar — not the full row.
3Full-row latest needs ROW_NUMBER or DISTINCT ON.
--key facts
DISTINCT limitdedupes, cannot choose survivor
ROW_NUMBERportable keep-one pattern
DISTINCT ONPG shortcut, non-portable
--use cases
Latest-per-user, top-per-group, dupe cleanup keep-one
DISTINCT ON shortcut where PG is guaranteed
--under the hood
ROW_NUMBER needs a sort per partition key — (partition, order) indexes feed it; DISTINCT ON uses same sort cheaper without window pass.
Add UNIQUE after cleanup or dupes recur — audit plus constraint.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.