SQL (Structured Query Language) UNION, UNION ALL, INTERSECT and EXCEPT for testers
Sometimes you need to join two result lists or find what is in one and not the other. Learn the four set operators and the count traps.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: two guest lists. You can stack both lists. You can keep only the names that are on both. You can keep the names that are on the first list and not the second. A name that is on both lists may be counted once or twice. That depends on the question.
A total of guests is wrong if one person is on both lists and is counted twice.
Daily List is a to-do list app. Tom, the tester, uses the same stacking to compare two task lists.
What it is: stack two results. UNION drops repeated rows. UNION ALL keeps them. answer the other two questions. INTERSECT keeps rows that are in both. EXCEPT keeps rows that are in the first and not the second. SQL (Structured Query Language) is the language these words belong to. A table is a grid, a row is one item, and a column is one fact about that item. These operators are easy to mix up with a join. A join matches columns across tables. These operators stack whole result lists.
Why it matters: The is a count that changes with the word you pick. Tom can report 5 tasks or 4 tasks from the same two lists. Only one of those numbers matches the question Nina asked.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)
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 Comparing two guest lists.
- Union
- Both
- Only
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| Both lists together | UNION |
| Names on both | INTERSECT |
| First list only | EXCEPT |
| Counting a person twice | UNION ALL |
WHERE THIS PICTURE STOPS
- A list has names, but queries need matching columns.
- A person is one, but a row may repeat.
- Lists are short, but tables are large.
HOOK
Both, either, one only.
TELL A FRIEND
Two guest lists: both, either, one only.
OFTEN MIXED UP WITH Looking at data with SQL SELECT. That topic reads one query. This one combines two queries.
PRACTISE AND KEEP
Sources for this topic — 3 sources
- Market practice
Combining Queries: UNION, INTERSECT, EXCEPT (PostgreSQL documentation) ↗ — PostgreSQL Global Development Group · PostgreSQL documentation: UNION, INTERSECT and EXCEPT combine the results of two queries, drop repeated rows unless ALL is used, and need queries with matching columns (search excerpt, page not opened)
PostgreSQL18 publisher documentation opened on6November2026: §7.4 UNION/INTERSECT/EXCEPT and ALL. No execution on PostgreSQL is claimed; engine-specific examples remain scoped to18.
- Market practice
SELECT: compound select statements (SQLite documentation) ↗ — SQLite project · SQLite documentation: compound select statements combine queries with UNION, UNION ALL, INTERSECT and EXCEPT (search excerpt, page not opened)
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.
- Implementation-specific
PostgreSQL documentation ↗ — The PostgreSQL Global Development Group · PostgreSQL 18 official manual, opened 2026-11-06: queries-union §7.4 and SQLite SELECT §3. 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.