Skip to content
Learn Netverks

Lesson

Step 23/36 64% through track

union-intersect

UNION and set operations

Last reviewed May 28, 2026 Content v20260528
Track mode
sql_sandbox
Means
SQL sandbox
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches UNION and set operations: the SQL patterns, schema habits, and query reasoning you need before advancing in SQL.

Teams query UNION and set operations on every SQL codebase—skipping it leaves gaps in debugging and data reviews.

You will apply UNION and set operations in contexts like: Postgres, MySQL, SQLite, warehouses, and ORMs that still expose SQL.

Copy SQL from each lesson into SQLite (sqlite3), DB Fiddle, or local Postgres—read result grids and row counts. The in-browser SQL lab (sql_sandbox) will run queries when the runner ships; until then, local clients are the practice path.

When you can explain the previous lesson's ideas without copying example queries verbatim.

Set operations combine result sets from multiple SELECTs with compatible columns. UNION merges rows; INTERSECT and EXCEPT compute overlap and difference.

UNION ALL

SELECT email FROM customers
UNION ALL
SELECT contact_email FROM suppliers;

Practice: Seed customers and orders sample data, then run each query. Verify row counts manually.

UNION ALL keeps duplicates; faster when you know duplicates are impossible or acceptable.

UNION dedupe

SELECT city FROM customers
UNION
SELECT city FROM suppliers;

UNION (without ALL) removes duplicate rows across branches—extra sort/hash cost.

INTERSECT and EXCEPT

SELECT customer_id FROM orders
INTERSECT
SELECT id FROM customers WHERE email LIKE '%@example.com';

SELECT customer_id FROM orders
EXCEPT
SELECT id FROM customers WHERE name = 'Ada';

Support is broad in Postgres and SQLite 3.39+; MySQL historically used workarounds—see MySQL track.

Column alignment

Each SELECT must return the same number of columns with compatible types. Names come from the first SELECT.

Important interview questions and answers

  1. Q: UNION vs UNION ALL?
    A: UNION removes duplicates; UNION ALL concatenates all rows.
  2. Q: UNION vs JOIN?
    A: UNION stacks rows vertically; JOIN combines columns horizontally.

Self-check

  1. When prefer UNION ALL over UNION?
  2. Must column counts match across UNION branches?

Tip: Prefer UNION ALL when duplicates are impossible—it skips dedup work.

Interview prep

UNION vs UNION ALL?

UNION deduplicates; UNION ALL concatenates all rows.

UNION vs JOIN?

UNION stacks rows vertically; JOIN combines columns horizontally.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • UNION ALL when?
  • Column count match?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump