SQL Semantics
UNION vs UNION ALL
UNION deduplicates, while UNION ALL preserves bag multiplicity.
UNION and UNION ALL differ
UNION deduplicates like a set operation, while UNION ALL keeps bag multiplicity. This is the capstone contrast with Book One set algebra. Note: the two rendered outputs have different cardinalities.
UNION deduplicates
The recomputed UNION row count is 3. Note: duplicates are removed by this operator.
SQL bag/multiset + three-valued NULL logic, deterministic but surprising; tiny finite tables; no engine/perf claims. Set algebra is Book 1.
UNION ALL keeps duplicates
The recomputed UNION ALL row count is 5. Note: ALL is the signal to preserve multiplicity.
SQL bag/multiset + three-valued NULL logic, deterministic but surprising; tiny finite tables; no engine/perf claims. Set algebra is Book 1.
NULL rows are still rows
In the NULL example, UNION recomputes to row count 3 with NULL rows 1, while UNION ALL recomputes to row count 5 with NULL rows 2. Note: ALL preserves both duplicate DB rows and duplicate NULL rows.
SQL bag/multiset + three-valued NULL logic, deterministic but surprising; tiny finite tables; no engine/perf claims. Set algebra is Book 1.
Summary
SQL can move between set-like and bag-preserving behavior depending on the operator. Note: bag and NULL semantics are here; set algebra was Book One.