Sub topic 7
How NULLs and duplicates are treated
Assume you can write basic SELECT queries with WHERE, understand tables, rows, and columns, and know the purpose of DISTINCT, ORDER BY, and NULL. This chapter focuses on how set operators handle repeated rows and missing values.
A set operator compares complete result rows. It does not compare only the first column unless the queries return only one column. For example, (1, 'open') and (1, 'closed') are different rows because one column differs.
Duplicate handling
UNION, INTERSECT, and EXCEPT remove duplicate rows by default. Their ALL forms preserve or count duplicates when the database supports them.
| Operator | Default result | Effect of duplicate input rows |
|---|---|---|
| `UNION` | Distinct rows from either query | Keeps one copy of each row |
| `UNION ALL` | Every row from both queries | Keeps every occurrence |
| `INTERSECT` | Distinct rows in both queries | Keeps one copy of each common row |
| `EXCEPT` | Distinct rows in the first query only | Keeps one copy of each qualifying row |
Consider two result sets containing email addresses:
WITH left_result(email) AS (
VALUES ('[email protected]'), ('[email protected]'), (NULL)
),
right_result(email) AS (
VALUES ('[email protected]'), ('[email protected]'), (NULL)
)
SELECT email FROM left_result
UNION
SELECT email FROM right_result;The result contains [email protected], [email protected], and one NULL. UNION treats repeated copies of the same complete row as duplicates.
Changing the operator to UNION ALL returns five rows. The two copies of [email protected] from the first query, the additional copy from the second query, [email protected], and NULL all remain.
NULL in set operations
NULL means that a value is missing or unknown. In an ordinary condition, NULL = NULL does not evaluate to true. It evaluates to unknown, so this condition does not find two missing emails:
WHERE customer_email = subscriber_emailSet operations use different rules for duplicate elimination and membership. For these purposes, NULL values are treated as matching each other. Therefore:
UNIONreturns oneNULLrow when either input contains one or both inputs contain one.INTERSECTreturns oneNULLrow when both inputs containNULL.EXCEPTremoves aNULLfrom the first result if the second result containsNULL.
The distinction matters when rewriting a set operation as a join or EXISTS query. A normal equality comparison does not match two NULL values. A rewrite must use a null-safe comparison when equivalent behavior is required.
INTERSECT and EXCEPT with duplicates
The default forms are distinct forms. If the database supports INTERSECT ALL and EXCEPT ALL, duplicates affect the output count.
For INTERSECT ALL, a row appears the smaller number of times it appears in either input. If a row appears three times on the left and twice on the right, it appears twice in the result.
For EXCEPT ALL, a row appears the left-side count minus the right-side count, never fewer than zero times. If a row appears four times on the left and once on the right, it appears three times.
Support for the ALL forms differs between database systems. Check the documentation for the database you use before relying on them.
Practical rule
Choose the operator based on whether row multiplicity is data:
- Use
UNIONwhen you need a deduplicated list. - Use
UNION ALLwhen every source row must remain visible. - Use
INTERSECTwhen you need distinct common rows. - Use
EXCEPTwhen you need distinct rows absent from the second result. - Use an
ALLvariant only when duplicate counts are meaningful and supported.
Exercise
If the first result contains two NULL rows and the second result contains one NULL row, how many NULL rows does UNION, UNION ALL, INTERSECT, and EXCEPT return?
Show answer
UNION returns one, UNION ALL returns three, INTERSECT returns one, and EXCEPT returns zero.