TOPIC 22/2816 MINNOT STARTED

Finding duplicates and orphan rows with a query, as a tester

After a save, an import or a move, ask the database two questions: is anything there twice, and does anything point at nothing? Learn both checks and how to report them.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: a cloakroom check. The attendant makes sure no coat has two tickets, and no ticket has lost its coat. An empty list of problems at the end of the night is a good night.

A problem is a coat with two tickets, or a ticket with no coat.

Daily List is a to-do list app. Tom, the tester, asks the database those two questions after a save.

What it is: A finds the same task saved twice. An finds a task whose list is missing. Both are written as an expected empty result. A pass is no rows. SQL (Structured Query Language) is the language the checks are written in. A table is a grid, a row is one item, and a column is one fact about that item. The easy mix-up is grouping by the id. Every id is already unique, so that group hides the repeat.

Why it matters: The screen can show one Buy milk while the table holds two. A later reminder can point at a task whose list is gone. Tom wants the query that fails in front of him before he trusts an empty result.

(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)

PLAIN WORDS · 1 in this section
cloakroom
a room where coats and bags are left safely

PICTURE IT

The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.

THE REAL JOB A cloakroom with no double tickets and no lost coats.

  1. Act
  2. Check
  3. Report
Cloakroom

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
A coat with two ticketsA duplicate row
A ticket with no coatAn orphan row
A clean nightAn expected empty result
The attendant checkingThe check query

WHERE THIS PICTURE STOPS

  • A cloakroom is small, but a table is large.
  • A coat is seen at once, but a row is found by a query.
  • A night ends, but a check can run any time.

HOOK

Two questions, empty lists.

TELL A FRIEND

A cloakroom: no coat twice, no ticket without a coat.

OFTEN MIXED UP WITH SQL (Structured Query Language) UNION, UNION ALL, INTERSECT and EXCEPT for testers. That topic combines query results. This one checks data for doubles and for rows with no parent.

PRACTISE AND KEEP

Sources for this topic — 3 sources
  1. Market practice

    Finding duplicate values in a database table (Atlassian data guide) ↗ — Atlassian · Atlassian guide: group by the columns that define a duplicate and keep groups with a count above one, leave out the key column because it is always unique, and add unique constraints as the lasting fix (search excerpt, page not opened)

    An Atlassian data guide that groups by the columns that define a duplicate and keeps groups with more than one row, and says unique constraints are the lasting fix. Vendor documentation. Practitioner guidance, not a standard. · Checked 2026-10-31

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  2. Market practice

    Aggregate functions (PostgreSQL tutorial) ↗ — PostgreSQL · PostgreSQL tutorial: a having clause filters groups after they are counted, where a where clause filters rows before (search excerpt, page not opened)

    The PostgreSQL tutorial page on grouping rows and filtering groups. Official project documentation. · Checked 2026-10-31

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.

  3. Implementation-specific

    PostgreSQL documentation ↗ — The PostgreSQL Global Development Group · PostgreSQL 18 official manual, opened 2026-11-06: ddl-constraints and queries-table-expressions. Supports only PostgreSQL behavior; earlier execution evidence remains separately versioned.

    A free, precise, section-numbered description of the SQL a payments practitioner actually writes. Join types and what happens to unmatched rows, GROUP BY and HAVING against WHERE, window functions and the de-duplication and running-total idioms built on them. Transaction isolation levels and the anomalies each permits, index use, and null three-valued logic.

    Used here for the data-and-SQL questions a Payment Operations Analyst, a Production Support Analyst and a Sanctions Screening Analyst are asked. · Checked 2026-08-18

    An implementation, not the standard — which is the point. ISO/IEC 9075-2 is paywalled, and this documents the same constructs in text anybody can open. Its isolation-level chapter attributes to the standard explicitly, so it can carry a standard-level claim as long as the citation says which document it is quoting.

    Cited as market practice rather than an official standard by kind: an engine's manual is authoritative about that engine and is not the obligation. Where a question turns on behaviour PostgreSQL chose rather than behaviour SQL requires, the answer has to say so.

    One correction carried here because it circulates widely: the sentence "The fundamental difference between WHERE and HAVING…", quoted all over the internet as PostgreSQL documentation, is not in the current documentation. It was searched for in the source tree and is not there.

    Read through a mirror, because the publisher's own domain could not be fetched (a reachability gap, not a statement about the publisher). A 403 there is a policy refusal and says nothing about whether the document exists or is reachable elsewhere.

    Anyone on an unrestricted network should open the canonical URL above and confirm the section and page numbers cited against it. Every claim drawn from this entry is written so that it can be checked against a numbered page.

    Read from the postgres/postgres documentation source tree (doc/src/sgml), which the project itself maintains and which carries the section ids the published URLs are derived from — so a cited section id can be checked against the rendered page.

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.