Finding duplicates and bad rows with a query
Duplicates and missing links are the most common data problems. Learn the group and count check, the join check for orphans, and how to report what you find.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a shelf check. A librarian looks for two copies with the same label, and for a book whose shelf card is missing. Each problem needs a different look. The list of problems goes to the head librarian.
Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Sam, the , looks for the same task saved twice, and for a task whose list does not exist.
What it is: (Structured Query Language) can group and join. A table is a grid, a row is one record, and a column is one fact. A groups by the columns that define "the same" and keeps groups with a count above one. An orphan row points at a parent that is not there. A data quality finding writes what was checked, how many rows failed, a few examples, and who owns the fix. A duplicate and an orphan are neighbours. One query will not find both.
Why it matters: Tasks 1 and 10 are both Buy milk for Lily, due the same day. A screen can show two tasks when Nina, the , thinks there is one. A task that points at a missing list will fail the moment the screen looks the list up.
Finding the rows is not the same job as cleaning them.
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the work.
THE REAL JOB A library shelf check for double copies and missing cards.
- Check
- Count
- Report
HOW THE PICTURE MAPS
| In the picture | In the work |
|---|---|
| Two copies, one label | Duplicate rows |
| A book with no shelf card | An orphan row |
| The list for the head librarian | A data quality finding |
| Checking the labels | A duplicate check |
WHERE THIS PICTURE STOPS
- A shelf has few books, but a table has millions of rows.
- A librarian sees the books, but a query sees counts.
- A missing card is easy to see, but an orphan needs a join.
HOOK
Group, count, join.
TELL A FRIEND
A shelf check: look for doubles and for missing cards.
OFTEN MIXED UP WITH SQL for BAs: JOIN. That topic joins tables. This one uses grouping and joins to find duplicates and orphan rows.
PRACTISE AND KEEP
Sources for this topic — 5 sources
- Market practice
Constraints (PostgreSQL documentation, data definition chapter) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §5.5 CHECK/NOT NULL/UNIQUE/primary/foreign keys; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §5.5 CHECK/NOT NULL/UNIQUE/primary/foreign keys. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
Table expressions and joined tables (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL 18, §7.2 joined tables, grouping and grouping sets; publisher page opened6November 2026. Dialect scope explicit.
PostgreSQL18 publisher documentation opened on6November2026: §7.2 joined tables, grouping and grouping sets. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
SQLite: SQL expressions ↗ — SQLite project · SQLite: SQL expressions; CASE/BETWEEN/IN/EXISTS, scalar cardinality, NULL-safe equality and parameter values. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Market practice
SQLite: foreign-key support ↗ — SQLite project · SQLite: foreign-key support; Connection-level enabling and referenced/referencing constraints. Publisher page opened6November 2026.
Publisher documentation opened on6November2026. Engine/version scope is explicit; other database implementations may differ. Teaching queries are synthetic, not a required publisher template.
- Simplified educational illustration
Payments Signal editorial teaching models — Payments Signal
What this simplifies: Daily List people, data and exercises are invented for teaching. Query results belong to the declared fixture and database version. Authorization, business identity, time bounds and unknown-value policy must be agreed in context; no universal mandatory query template is implied.
Used wherever diagrams, scenarios, figures, or example values are didactic constructions rather than sourced facts; every such use carries a simplifications disclosure. All people, companies, banks, and list entries in examples are fictional.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.