Track 0/9 (0%)
Module 07 · Set Algebra~14 min · 4 worked queries
Set Algebra: UNION, INTERSECT & EXCEPT
Combine aligned result sets vertically while controlling duplicate preservation versus mathematical set deduplication.
UNION ALL appends result streams directly; UNION, INTERSECT, and EXCEPT compare full tuples across aligned schema positions.Module 07 · Set Algebra · Reading Room
Mental Model
- 01.
Set operators require equal column counts and compatible types across left and right SELECT branches.
- 02.
Prefer UNION ALL whenever duplicates are impossible or desired, skipping the duplicate-elimination pass.
- 03.
INTERSECT isolates shared tuples present in both streams; EXCEPT subtracts right tuples from the left set.
Interactive Module Lab · Set Operations
37. UNION removes duplicates — sqlSELECT country AS place FROM usersUNIONSELECT location FROM departmentsORDER BY place;
Execution Stages5 steps
Stage 01 · FROM
10 in → 10 outSOURCE: USERS
Scanned 10 rows from table 'users'.
| # | id | name | department_id | role | salary | state |
|---|---|---|---|---|---|---|
| 1 | 1 | Alice Smith | 1 | Software Engineer | 95,000 | keep |
| 2 | 2 | Bob Jones | 2 | Product Manager | 110,000 | keep |
| 3 | 3 | Charlie Lee | 1 | Software Engineer | 88,000 | keep |
| 4 | 4 | Diana Prince | 3 | UX Designer | 76,000 | keep |
| 5 | 5 | Evan Wright | 2 | Data Analyst | 82,000 | keep |
| 6 | 6 | Fiona Gallagher | 1 | Tech Lead | 135,000 | keep |
| 7 | 7 | George Clark | NULL | Intern | 42,000 | keep |
| 8 | 8 | Hannah Abbott | 2 | Scrum Master | 89,000 | keep |
Inspect each evaluated clause in 37. UNION removes duplicates or animate every row moving between stages in Studio.Open 37. UNION removes duplicates in Studio →