Reading Room · Module 07
Track 0/9 (0%)
Course completion
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.

Trace Live in StudioAll Modules
Core Relational Rule
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

  1. 01.

    Set operators require equal column counts and compatible types across left and right SELECT branches.

  2. 02.

    Prefer UNION ALL whenever duplicates are impossible or desired, skipping the duplicate-elimination pass.

  3. 03.

    INTERSECT isolates shared tuples present in both streams; EXCEPT subtracts right tuples from the left set.

Frequent Pitfall to Avoid
Forgetting that UNION deduplicates rows even across sources where duplicate counting matters.

Interactive Module Lab · Set Operations

37. UNION removes duplicates — sql
SELECT country AS place FROM users
UNION
SELECT location FROM departments
ORDER BY place;
Execution Stages5 steps
Stage 01 · FROM

SOURCE: USERS

10 in → 10 out

Scanned 10 rows from table 'users'.

#idnamedepartment_idrolesalarystate
11Alice Smith1Software Engineer95,000keep
22Bob Jones2Product Manager110,000keep
33Charlie Lee1Software Engineer88,000keep
44Diana Prince3UX Designer76,000keep
55Evan Wright2Data Analyst82,000keep
66Fiona Gallagher1Tech Lead135,000keep
77George ClarkNULLIntern42,000keep
88Hannah Abbott2Scrum Master89,000keep
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 →

Worked Presets in This Module

36. Set Operations (UNION ALL)

Run two queries and stack the results. UNION ALL keeps every row, duplicates included.

37. UNION removes duplicates

Same stacking as UNION ALL, then identical rows are collapsed into one.

38. INTERSECT: in both results

Keeps only the values that appear in both queries.

39. EXCEPT: in the first but not the second

Which departments have no employees at all?

Navigation

LivePreparation

Client-side workspace · saved locally