Sub topic 11
INTERSECT ALL and EXCEPT ALL
Assume you can write basic SELECT queries and understand DISTINCT, NULL, and ordinary INTERSECT and EXCEPT. This chapter focuses on the ALL variants, which preserve some duplicate rows instead of treating each result as a pure set.
Why the `ALL` variants exist
The ordinary set operators remove duplicate rows:
INTERSECTreturns each matching row once.EXCEPTreturns each remaining row once.
The ALL variants treat results as multisets, sometimes called bags. A multiset records how many times a row occurs.
For a row that appears m times in the first query and n times in the second:
| Operator | Number of copies returned |
|---|---|
| `INTERSECT ALL` | `MIN(m, n)` |
| `EXCEPT ALL` | `MAX(m - n, 0)` |
This behavior matters when duplicate rows represent quantities, repeated events, or other meaningful counts.
`INTERSECT ALL`
INTERSECT ALL returns matching rows while preserving the lower duplicate count from the two queries.
Suppose the first query produces:
red
red
red
blueThe second produces:
red
red
greenINTERSECT returns one red. INTERSECT ALL returns two red rows because red appears three times on the left and two times on the right.
SELECT color FROM first_batch
INTERSECT ALL
SELECT color FROM second_batch;The two queries still need compatible column lists. They must return the same number of columns, with compatible data types in corresponding positions.
`EXCEPT ALL`
EXCEPT ALL subtracts duplicate counts in the second result from duplicate counts in the first. It keeps the order of the operation: the left query is the source, and the right query removes matching copies.
If the first query returns three copies of red and the second returns one, EXCEPT ALL returns two copies of red.
SELECT color FROM first_batch
EXCEPT ALL
SELECT color FROM second_batch;Swapping the queries changes the result:
SELECT color FROM second_batch
EXCEPT ALL
SELECT color FROM first_batch;The second version returns no red rows when the second batch has fewer copies than the first. EXCEPT ALL never produces a negative number of rows.
How the counts work
The following example makes the difference between ordinary and ALL operators visible:
WITH left_values(value) AS (
VALUES ('A'), ('A'), ('A'), ('B')
),
right_values(value) AS (
VALUES ('A'), ('A'), ('C')
)
SELECT value
FROM left_values
INTERSECT ALL
SELECT value
FROM right_values;The result contains two A rows. The left side has three, and the right side has two.
Changing the operator shows subtraction:
WITH left_values(value) AS (
VALUES ('A'), ('A'), ('A'), ('B')
),
right_values(value) AS (
VALUES ('A'), ('A'), ('C')
)
SELECT value
FROM left_values
EXCEPT ALL
SELECT value
FROM right_values;The result contains one A and one B.
A useful mental model is to count each distinct row before combining the results:
Dialect and portability concerns
Support for INTERSECT ALL and EXCEPT ALL differs between database systems and versions. Some systems support the ordinary operators but reject one or both ALL forms. Oracle commonly uses MINUS instead of EXCEPT, and support for its ALL form depends on the database version.
Check your database documentation before relying on these operators in portable application code. If an ALL variant is unavailable, you can often reproduce its behavior with window functions such as ROW_NUMBER() and a join, but that rewrite is longer and needs careful handling of NULL values.
Exercise
The left query contains four copies of X, and the right query contains two. How many X rows does INTERSECT ALL return, and how many does EXCEPT ALL return?
Show answer
INTERSECT ALL returns two. EXCEPT ALL returns two because it subtracts two right-side copies from four left-side copies.