TOPIC 16/3015 MINNOT STARTED

SQL for BAs: UNION, EXCEPT and EXISTS

Sometimes the question is which rows are in one list and not in another. Learn the set operators and EXISTS, and the traps that come with them.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: two guest lists. You can ask who is on either list. You can ask who is on both, and who is on the first list only. To ask whether a guest has any gift, you look for a gift for that guest. You do not need the gift's details.

Daily List is a made-up to-do app (SYNTHETIC / TRAINING ONLY). Sam, the , wants the people who have no task, and he wants to stack a paid-plan list with an invite list.

What it is: (Structured Query Language) can combine two query results. A table is a grid, a row is one record, and a column is one fact. are the set operators. UNION stacks and drops repeats. INTERSECT keeps rows that are in both. EXCEPT keeps rows from the first query that are not in the second. EXISTS asks whether a second query finds any row. NOT IN and NULL is the trap: if the list holds a null, NOT IN does not return the rows you expect. NOT EXISTS is the safer neighbour.

Why it matters: Mei is the only person with no task. A NOT IN list that secretly holds a null returns nobody. Sam would tell Nina that every person has a task.

UNION ALL keeps repeats. Plain UNION spends work removing 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 Comparing two guest lists.

  1. A
  2. B
  3. Gap
Guest lists

HOW THE PICTURE MAPS

How each part of the picture matches the work
In the pictureIn the work
First listQuery A
Second listQuery B
On the first but not the secondEXCEPT
Checking the table for each guestEXISTS

WHERE THIS PICTURE STOPS

  • A guest list has names, but queries have columns that must match.
  • A guest list has no hidden blanks, but data has NULL.
  • Lists are short, but tables are large.

HOOK

In A, not in B.

TELL A FRIEND

Two guest lists: both, either, or one only.

OFTEN MIXED UP WITH SQL for BAs: JOIN. That topic matches rows across tables. This one finds rows that have no match, and combines lists.

PRACTISE AND KEEP

Sources for this topic — 4 sources
  1. Market practice

    Combining Queries: UNION, INTERSECT, EXCEPT (PostgreSQL documentation) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §7.4 UNION/INTERSECT/EXCEPT and ALL; publisher page opened6November 2026. Dialect scope explicit.

    §7.4 UNION/INTERSECT/EXCEPT and ALL. PostgreSQL18 implementation, not a universal SQL mandate. · Checked 2026-11-06

    PostgreSQL18 publisher documentation opened on6November2026: §7.4 UNION/INTERSECT/EXCEPT and ALL. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.

  2. Market practice

    Subquery Expressions (PostgreSQL documentation) ↗ — PostgreSQL Global Development Group · PostgreSQL 18, §9.24 EXISTS/IN/NOT IN NULL semantics; publisher page opened6November 2026. Dialect scope explicit.

    §9.24 EXISTS/IN/NOT IN NULL semantics. PostgreSQL18 implementation, not a universal SQL mandate. · Checked 2026-11-06

    PostgreSQL18 publisher documentation opened on6November2026: §9.24 EXISTS/IN/NOT IN NULL semantics. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.

  3. Market practice

    SELECT: compound select statements (SQLite documentation) ↗ — SQLite project · SQLite rolling SELECT documentation opened6November 2026; §§2–5 joins/grouping/bare columns/compound queries/order/limit. UNION ALL supported; INTERSECT ALL/EXCEPT ALL are not. Synthetic fixtures rechecked on3.53.1; historical3.40.1 observations retain their date/version.

    Says EXCEPT returns the rows from the left query that the right query does not produce, and that both sides need the same number of columns. · Checked 2026-11-06

    SQLite rolling SELECT documentation opened6November2026; §§2–5 joins/grouping/bare columns/compound queries/order/limit. UNION ALL supported; INTERSECT ALL/EXCEPT ALL are not. Synthetic fixtures rechecked on3.53.1; historical3.40.1 observations retain their date/version.

  4. Simplified educational illustration

    Payments Signal editorial teaching models — Payments Signal

    This site's own simplified teaching models. · Checked 2026-07-12

    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.