# UNION, INTERSECT & EXCEPT

> Stack, overlap and subtract the results of two queries with UNION, INTERSECT and EXCEPT, and know when duplicates stay.

Subject: SQL | Lesson 17 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/set-operations
Previous: [Joining Many Tables](https://codewithanimation.in/sql/joining-many-tables.md) | Next: [Join Pitfalls: Duplicates & Fan-Out](https://codewithanimation.in/sql/join-pitfalls.md)

## Combine rows, not columns

A join makes a table wider. A **set operation** combines the rows of two queries, one on top of the other. The two queries must return the same number of columns, with compatible types.

`UNION` stacks them and removes duplicate rows. `UNION ALL` stacks them and keeps everything. `INTERSECT` keeps only the rows found in both. `EXCEPT` keeps the rows of the first query that are not in the second.

## Questions about two lists

"Who is in either club?", "Who is in both?" and "Who is only in the first?" are questions about two lists. Each of them has its own operator.

## Two clubs, one member in both

```sql
SELECT name FROM club_a
UNION
SELECT name FROM club_b;
```

`club_a` has Asha, Ben and Chen. `club_b` has Chen, Dara and Eli. `UNION` gives 5 names, with Chen once. `UNION ALL` gives 6 names, with Chen twice. `INTERSECT` gives only Chen. `club_a EXCEPT club_b` gives Asha and Ben.

For removing duplicates, two `NULL` values count as equal, just like in `DISTINCT`. That is different from a normal comparison, where NULL is never equal to NULL.

## Common mistake: Order, columns and speed

`EXCEPT` depends on order: A minus B is not B minus A. `UNION` and `INTERSECT` give the same rows in either order.

`UNION` has to find duplicates, which costs extra work. If you know the two lists cannot overlap, `UNION ALL` is cheaper and gives the same answer.

The columns are matched by position, not by name, and the names of the result come from the first query. If you mix up the column order, the values land in the wrong column.

When you mix operators, `INTERSECT` binds tighter than `UNION` and `EXCEPT`. Use brackets to make your meaning clear.

## Tip: Default to UNION ALL when you know there are no overlaps

Choose `UNION` only when duplicates are possible and unwanted. Write the same column list in both queries, in the same order, and add `ORDER BY` once at the very end, where it sorts the combined result.

## Try it: Combine two clubs

Press each operator and watch Chen. Then compare the two EXCEPT versions to see why order matters.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/set-operations

## Check yourself

### Question 1

`club_a` has 3 names and `club_b` has 3 names, with Chen in both. How many rows does `UNION ALL` return?

- A. 6
- B. 5
- C. 1

Correct answer: A

UNION ALL appends all rows without looking for duplicates: 3 plus 3 is 6.

- Why not B: 5 is the UNION count. UNION ALL keeps the duplicate Chen.
- Why not C: 1 is the INTERSECT count. UNION ALL stacks every row of both lists.

### Question 2

Which query returns Dara and Eli from the two clubs?

- A. club_b EXCEPT club_a
- B. club_a EXCEPT club_b
- C. club_a INTERSECT club_b

Correct answer: A

EXCEPT keeps the rows of the first query that are missing from the second, so the order of the queries matters.

- Why not B: That returns Asha and Ben, the names only in club_a.
- Why not C: That returns only Chen, the name in both.

### Question 3

Two lists cannot share any rows. Which operator should you use to stack them, and why?

- A. UNION ALL, because it gives the same rows and skips the duplicate search
- B. UNION, because ALL is not safe
- C. INTERSECT, because it keeps everything

Correct answer: A

With no overlap, UNION removes nothing, so UNION ALL gives the same answer with less work.

- Why not B: UNION ALL is safe. It only skips the duplicate removal.
- Why not C: INTERSECT keeps only rows found in both lists.

## Remember this

- UNION, INTERSECT and EXCEPT combine the rows of two queries that have the same columns.
- UNION and INTERSECT and EXCEPT remove duplicates unless you write ALL.
- EXCEPT depends on order: A minus B is different from B minus A.
- Columns are matched by position, and ORDER BY at the end sorts the combined result.
