Quick revision
The short answer
UNION stacks two result sets and then removes duplicate rows, which costs a sort or a hash over the whole result; UNION ALL simply stacks them and keeps every row. INTERSECT returns the distinct rows that appear in both inputs, and EXCEPT returns the distinct rows of the first input that are not in the second. Using UNION where rows are genuinely repeated, such as two equal payments, silently drops data.
What to remember
In this answer, we saw that UNION removes duplicate rows and pays for it with a sort or hash, while UNION ALL keeps every row and streams them. INTERSECT and EXCEPT compare whole rows and return distinct results, and their ALL forms keep the counts. Columns are matched by position with compatible types, NULLs count as equal for duplicate removal, and MySQL has INTERSECT and EXCEPT since 8.0.31. The payment example showed why UNION ALL should be the default: two genuine payments with the same date and amount are not duplicates.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
UNION stacks two result sets and then removes duplicate rows, which costs a sort or a hash over the whole result; UNION ALL simply stacks them and keeps every row. INTERSECT returns the distinct rows that appear in both inputs, and EXCEPT returns the distinct rows of the first input that are not in the second. Using UNION where rows are genuinely repeated, such as two equal payments, silently drops data.
Why this question is asked
Set operations look like small syntax choices, but they decide whether a report counts every row or quietly collapses some of them. Interviewers ask about UNION versus UNION ALL because the wrong choice produces numbers that look plausible and are wrong, and because the difference also shows whether we think about cost: duplicate removal is real work on a large result. In this answer, we will recap what a set operation does, compare UNION and UNION ALL, look at INTERSECT and EXCEPT with their ALL variants, see the rules for matching columns and NULLs, and finish with a payment report where UNION is a real bug.
1A Quick Recap of Set Operations
A set operation combines the results of two complete SELECT statements into one result. Unlike a join, which puts columns side by side, a set operation puts rows on top of each other or compares whole rows. Each input is a list of rows, and the operation decides which rows survive.
There are three operations. UNION keeps rows from either input. INTERSECT keeps rows found in both. EXCEPT keeps rows of the first input that the second input does not have. Each one has two forms: the plain form treats the inputs as sets and removes duplicates, and the ALL form treats them as bags, where the same row may appear several times and the count matters.
2UNION Versus UNION ALL
Let us take two tiny tables. card_payments holds amounts 50 and 20, and bank_payments holds amounts 20 and 30. The amount 20 appears once in each table.
Notice that UNION returned three rows. To remove duplicates, the database has to compare every row with every other row, and it does that by sorting the combined result or by building a hash table of rows it has already seen. On a few rows this is free. On ten million rows it means a large sort that may spill to disk, while UNION ALL can stream rows straight through without remembering anything.
So the rule of thumb is simple: write UNION ALL by default, and write UNION only when we have a reason to remove duplicates and we know what a duplicate means in our data. If the two inputs cannot overlap, for example because each one filters a different status, UNION buys nothing and still pays for the sort.
3INTERSECT and EXCEPT, With and Without ALL
INTERSECT and EXCEPT compare rows instead of stacking them. Here are both forms on two small lists, a = (1, 1, 2, 3) and b = (1, 3, 3, 4).
| Operation | Result | Rule |
|---|---|---|
| UNION | 1, 2, 3, 4 | rows in either input, duplicates removed |
| UNION ALL | 1, 1, 2, 3, 1, 3, 3, 4 | all rows of both inputs |
| INTERSECT | 1, 3 | distinct rows present in both |
| INTERSECT ALL | 1, 3 | each row min(count in a, count in b) times |
| EXCEPT | 2 | distinct rows of a missing from b |
| EXCEPT ALL | 1, 2 | count in a minus count in b, if positive |
The ALL variants keep multiplicities. INTERSECT ALL keeps a row as many times as the smaller count in the two inputs, and EXCEPT ALL subtracts counts: a has the value 1 twice and b has it once, so one 1 survives. The plain forms first reduce each input to distinct rows, so the counts are lost.
EXCEPT is very useful for checks such as which rows of a source did not arrive in a target, because it compares whole rows in one short statement. PostgreSQL supports all of these. MySQL added INTERSECT and EXCEPT in 8.0.31, so MySQL 8.4 has them; on older MySQL versions we had to write NOT EXISTS or a LEFT JOIN instead.
4How Columns and NULLs Are Matched
Set operations match columns by position, not by name. The first column of the first query is combined with the first column of the second query, whatever they are called, and the result takes its column names from the first query. Both inputs must have the same number of columns, and each pair of columns must have compatible types, or the database raises an error.
The silent case is the dangerous one. If two columns happen to have the same type, swapping their order produces no error, only a nonsense result. Listing columns explicitly in the same order on both sides, and never using SELECT star in a set operation, avoids this.
NULLs behave differently here than in a WHERE clause. In a comparison, NULL = NULL is not true, it is unknown. But for duplicate removal, set operations treat two NULLs as the same value, so (NULL, 'x') from both inputs counts as one row in UNION and as a match in INTERSECT. This is the same rule DISTINCT uses: rows that are not distinct from each other are duplicates.
5When UNION Is a Bug: Summing Two Payment Tables
Here is a real kind of mistake. A finance report adds up payments that live in two tables, card payments and bank transfers, for one day. A developer writes UNION because it is the word they remember.
- Select date and amount from both tables3 rows: 49.90, 49.90, 120.00
- UNION compares whole rowstwo rows are (2025-03-01, 49.90)
- Duplicates removedone 49.90 row is discarded
- SUM runs on 2 rows169.90 instead of 219.80
4 steps in order. Play them, or step through them one at a time.
The two card payments are different payments from different customers, but the selected columns, date and amount, are identical. UNION has no way to know that, so it treats them as one row and removes the second. Plans with a fixed price make this common: on a busy day hundreds of payments share the same date and amount.
The fix is UNION ALL. If we really do need to remove duplicates, for example the same payment imported twice, then we must include the column that identifies a payment, such as its id or reference, so that only true duplicates collapse. Duplicate removal should be a decision about identity, not a side effect of a keyword.
6Follow-up questions
Is UNION ever faster than UNION ALL?
Practically never on its own, because UNION does everything UNION ALL does and then removes duplicates. The only case where UNION can lead to a cheaper overall query is when the deduplicated result is much smaller and something expensive runs on it afterwards, but even then an explicit DISTINCT or GROUP BY on the key we care about says the intent more clearly.
Can ORDER BY and LIMIT be used with a set operation?
Yes. An ORDER BY or LIMIT written at the end applies to the combined result, and it can refer to the column names of the first query. To sort or limit one input before combining, we put that input in parentheses or in a subquery. Without an ORDER BY at the end, the order of rows from a set operation is not guaranteed.
How do I find rows that differ between two tables in both directions?
Run EXCEPT twice, a EXCEPT b and b EXCEPT a, and label each result. If the tables share a key, a FULL OUTER JOIN on that key gives clearer output, because a changed row shows up once with both versions instead of once as missing and once as extra.
7Conclusion
In this answer, we saw that UNION removes duplicate rows and pays for it with a sort or hash, while UNION ALL keeps every row and streams them. INTERSECT and EXCEPT compare whole rows and return distinct results, and their ALL forms keep the counts. Columns are matched by position with compatible types, NULLs count as equal for duplicate removal, and MySQL has INTERSECT and EXCEPT since 8.0.31. The payment example showed why UNION ALL should be the default: two genuine payments with the same date and amount are not duplicates.
Open with
UNION and UNION ALL both stack the rows of two queries, but UNION then removes duplicate rows, which means the database has to sort or hash the whole combined result.
Then, if they want more
UNION ALL keeps every row, so it is cheaper and it is what I use by default. INTERSECT gives the distinct rows that appear in both inputs, and EXCEPT gives the distinct rows of the first input that are missing from the second. Each has an ALL form that keeps multiplicities, so EXCEPT ALL actually subtracts counts.
A few rules matter: columns are matched by position, not by name, so both sides need the same number of columns in the same order with compatible types, and for duplicate removal two NULLs count as equal, unlike in a WHERE comparison. MySQL supports INTERSECT and EXCEPT since 8.0.31.
The classic bug is a finance report that sums payments from a card table and a bank table with UNION. Two different customers who paid 49.90 on the same day produce identical rows, UNION drops one of them, and revenue is understated with no error at all.
The fix is UNION ALL, and if I really need deduplication, I include the payment id so only true duplicates collapse.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- MySQL 8.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, The SQL Language
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is the difference between UNION and UNION ALL, and what do INTERSECT and EXCEPT...": https://scrivio-interview-library.netlify.app/databases/sql-fundamentals/0e054a205a/