Anti-joins and the NOT IN trap: finding rows with no match
Which users have no task? Learn three ways to ask, why one can return nothing when a null is present, and how to test a no-match report.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a door list. To find guests who are not on the list, you can check each guest against the list. If one line on the list is an unreadable blank, a careless rule can say that nobody is missing. The blank does not name a guest. It spoils the whole question.
Daily List is a to-do list app. Tom wants the users who have no task. Mei is one of them. One task has no user at all, and that empty owner is the blank line. (SYNTHETIC / TRAINING ONLY: Daily List and the people here are invented.).
What it is: An lists rows in one table that have no partner in another. (Structured Query Language) is the language of the question. A table is a grid. A row is one user or one task. A column is one fact, such as user_id. Three forms ask the question: a left join plus IS NULL, an existence test with NOT EXISTS, and NOT IN. The neighbour to fear is NOT IN beside NOT EXISTS. They look alike. A null trap in a filter makes NOT IN disagree when the list holds a null.
Why it matters: The report of users with no task can come back empty. Mei is left off, and the screen shows no error. Tom can miss a real gap in the data.
PLAIN WORDS · 1 in this section
- unreadable
- too unclear to make out
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 door list with one unreadable blank line.
- Add
- Run
- Count
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| A guest not on the list | A row with no match |
| Checking each guest | A left join |
| Asking if a name is listed | NOT EXISTS |
| A unreadable blank line | A null in the list |
WHERE THIS PICTURE STOPS
- A smudge is visible, but a null is silent.
- A guest list is short, but tables are large.
- A doorman decides, but a query follows rules.
HOOK
Torn stub on purpose.
TELL A FRIEND
A door list: test with a unreadable line.
OFTEN MIXED UP WITH NULL: the value that is not a value. That topic covers nulls in queries. This one tests no-match reports and the NOT IN trap.
PRACTISE AND KEEP
Sources for this topic — 3 sources
- Market practice
Subquery expressions (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL reference: NOT IN returns null, not true, when the subquery contains a null and no equal value is found, and EXISTS depends only on whether the subquery returns any row (search excerpt, page not opened)
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.
- Market practice
Table expressions and joined tables (PostgreSQL documentation) ↗ — PostgreSQL · PostgreSQL reference: a left join fills the right side with nulls when nothing matches, which a test for null can then pick out (search excerpt, page not opened)
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.
- Implementation-specific
PostgreSQL documentation ↗ — The PostgreSQL Global Development Group · PostgreSQL 18 official manual, opened 2026-11-06: functions-subquery §9.24 and functions-comparison. Supports only PostgreSQL behavior; earlier execution evidence remains separately versioned.
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/postgresdocumentation 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.