Sunday, November 29, 2026

UNION vs UNION ALL vs EXCEPT: Why Does Your SQL Return a Different Number of Rows?

You combine two SQL query results. One version returns eight rows; another returns four. Nothing is missing from the database: changing UNION ALL to UNION changed how duplicate rows are handled.

This guide shows how PostgreSQL combines and compares results, using overlapping circles and a runnable example. You will also see where Oracle's MINUS fits and why reversing the inputs changes an EXCEPT result.

Watch the tutorial

See the diagrams and examples explained step by step in the full YouTube tutorial. For a quick comparison, watch the UNION vs UNION ALL Short.

What you need

Use PostgreSQL and a SQL client such as the pgAdmin Query Tool. Each SQL block is self-contained: copy and execute a complete block at a time. WITH names the two temporary query results for that statement; VALUES supplies the sample rows. No persistent tables are created or changed.

The examples were executed against PostgreSQL 17.5 through PGlite, an in-memory WebAssembly build of PostgreSQL. Results are presented in sorted order because the queries end with ORDER BY. Oracle behavior below is based on its documentation; it was not tested on an Oracle server.

The two inputs

Our first result, A, contains four one-column rows: 1, 1, 2, NULL. The second result, B, also has four rows: 1, 3, NULL, NULL. A contains two copies of 1; B contains two NULL rows.

In the diagrams, the left-only region contains 2, the overlapping region contains 1 and NULL, and the right-only region contains 3. The circles show whether a value occurs on each side; they do not show the number of copies. The rows next to the diagram make those copies visible.

UNION ALL: keep every rowA21NULLB3A: 1, 1, 2, NULLB: 1, 3, NULL, NULLResult:1 · 1 · 1 · 2 · 3 · NULL · NULL · NULL8 rows: 1 appears 3 times; NULL appears 3 times.Circles show distinct values, not copy counts.Without ORDER BY, row order is not guaranteed.
Both circles are included; the result shows every copy.

UNION ALL: keep every row

WITH a(value) AS (VALUES (1), (1), (2), (NULL::integer)),
     b(value) AS (VALUES (1), (3), (NULL::integer), (NULL::integer))
SELECT value FROM a
UNION ALL
SELECT value FROM b
ORDER BY value NULLS LAST;

Result: 1, 1, 1, 2, 3, NULL, NULL, NULL — eight rows. UNION ALL preserves all four rows from A and all four from B. It does not remove repeated rows, whether those repetitions were already in one input or occur across both inputs.

UNION: remove duplicate result rows

WITH a(value) AS (VALUES (1), (1), (2), (NULL::integer)),
     b(value) AS (VALUES (1), (3), (NULL::integer), (NULL::integer))
SELECT value FROM a
UNION
SELECT value FROM b
ORDER BY value NULLS LAST;

Result: 1, 2, 3, NULL — four rows. All values present in either input remain, but UNION removes duplicate result rows. That's why the shaded circle regions are conceptually the same as for UNION ALL, even though the number of returned rows differs.

In this example a row has one column. With multiple selected columns, duplicate elimination compares the whole selected row. Two rows with the same customer ID and different course names are not duplicate result rows.

EXCEPT: rows in the first result but not the second

WITH a(value) AS (VALUES (1), (1), (2), (NULL::integer)),
     b(value) AS (VALUES (1), (3), (NULL::integer), (NULL::integer))
SELECT value FROM a
EXCEPT
SELECT value FROM b
ORDER BY value NULLS LAST;

Result: 2. 1 and NULL also occur in B, so they are excluded; 3 occurs only in B, so it was never a candidate from A. Plain EXCEPT returns distinct result rows.

A EXCEPT B: distinct differenceA21NULLB3A: 1, 1, 2, NULLB: 1, 3, NULL, NULLResult:21 and NULL occur in both and are excluded.Circles show distinct values, not copy counts.Without ORDER BY, row order is not guaranteed.
The highlighted left region remains; duplicates are removed.

Order matters

Switch the SELECT statements and the question changes from “what is in A but not B?” to “what is in B but not A?”

WITH a(value) AS (VALUES (1), (1), (2), (NULL::integer)),
     b(value) AS (VALUES (1), (3), (NULL::integer), (NULL::integer))
SELECT value FROM b
EXCEPT
SELECT value FROM a
ORDER BY value NULLS LAST;

Result: 3. A EXCEPT B and B EXCEPT A generally answer different questions.

EXCEPT ALL: subtract copies

PostgreSQL also supports EXCEPT ALL:

WITH a(value) AS (VALUES (1), (1), (2), (NULL::integer)),
     b(value) AS (VALUES (1), (3), (NULL::integer), (NULL::integer))
SELECT value FROM a
EXCEPT ALL
SELECT value FROM b
ORDER BY value NULLS LAST;

Result: 1, 2. For each distinct row, subtract the number of copies in B from the number in A, without going below zero. A contains two copies of 1 and B contains one, so one remains. A's single 2 remains; its single NULL is matched by a NULL in B and does not remain. The extra NULL in B does not produce a negative number of rows.

This is why a simple “A-only” shaded circle is insufficient for EXCEPT ALL: 1 appears in both input circles, yet one copy remains in the output.

A EXCEPT ALL B: subtract copiesA21NULLB3A: 1, 1, 2, NULLB: 1, 3, NULL, NULLResult:1 · 2A has 1 NULL; B has 2: zero NULLs remain.Circles show distinct values, not copy counts.Without ORDER BY, row order is not guaranteed.
The shared 1 still has one copy left: 2 copies minus 1 copy.

What about NULL?

In these set operations, PostgreSQL treats NULLs as matching for duplicate elimination and matching rows. Thus UNION shows one NULL here, while EXCEPT removes A's NULL because B contains one too. This does not mean the predicate NULL = NULL evaluates to true in a WHERE clause: SQL uses three-valued logic for that comparison.

PostgreSQL EXCEPT and Oracle MINUS

If you use Oracle, MINUS is the familiar operator for the distinct difference. In PostgreSQL, use EXCEPT; PostgreSQL does not use MINUS as a set operator. Oracle 19c documents MINUS, while Oracle Database 26 also documents EXCEPT as a synonym. Check your database version and its documentation when moving SQL between engines; the CTE and cast syntax in the examples above is PostgreSQL syntax.

Three practical rules

  1. Same number of columns: both input queries must return the same number of columns, with compatible types in corresponding positions.
  2. Whole rows matter: UNION and EXCEPT compare all columns in the selected row, not an individual column unless it is the only selected column.
  3. Order is not automatic: if result order matters, use an appropriate final ORDER BY. SQL does not promise that A's rows will appear before B's just because the query uses UNION ALL.

A quick exercise

Before running the query, predict the result of B EXCEPT ALL A using the same two inputs. Then swap the two SELECT statements in the EXCEPT ALL example. Remember to subtract copies for each row separately.

Show the expected answer

The expected result is 3, NULL: B has one 3 that is absent from A, and two NULLs compared with A's one NULL, leaving one copy. B's one 1 is matched by A's copies.

Continue learning PostgreSQL

For more hands-on SQL and PostgreSQL examples, explore my Practical SQL with PostgreSQL course on Udemy. This is a referral link to my course.

Official references

No comments:

Post a Comment