INTERSECT and EXCEPT in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to run INTERSECT and EXCEPT queries changes as the size of the data grows.
Specifically, how does the database find common or different rows between two sets?
Analyze the time complexity of the following SQL queries using INTERSECT and EXCEPT.
SELECT column1 FROM tableA
INTERSECT
SELECT column1 FROM tableB;
SELECT column1 FROM tableA
EXCEPT
SELECT column1 FROM tableB;
These queries find rows common to both tables (INTERSECT) or rows in the first table but not in the second (EXCEPT).
Both INTERSECT and EXCEPT require comparing rows from two tables.
- Primary operation: Comparing each row from one table to rows in the other table.
- How many times: For each row in the first table, the database checks for matching rows in the second table.
As the number of rows in the tables increases, the number of comparisons grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 100 comparisons |
| 100 | About 10,000 comparisons |
| 1000 | About 1,000,000 comparisons |
Pattern observation: The work grows quickly, roughly by the product of the input sizes.
Time Complexity: O(n * m)
This means the time to run the query grows roughly with the product of the sizes of the two tables.
[X] Wrong: "INTERSECT and EXCEPT just scan one table, so they are fast regardless of size."
[OK] Correct: Both operations must compare rows between two tables, so the work depends on both table sizes, not just one.
Understanding how INTERSECT and EXCEPT scale helps you explain query performance clearly and shows you can think about data size impact in real projects.
What if we added indexes on the columns used in INTERSECT and EXCEPT? How would the time complexity change?
Practice
INTERSECT operator do in PostgreSQL?Solution
Step 1: Understand INTERSECT purpose
INTERSECT returns only rows that appear in both query results.Step 2: Compare with other set operations
Unlike UNION or EXCEPT, INTERSECT finds common rows, not all or unique rows.Final Answer:
Returns rows common to both queries -> Option DQuick Check:
INTERSECT = common rows [OK]
- Confusing INTERSECT with UNION
- Thinking INTERSECT returns all rows from first query
- Mixing INTERSECT with EXCEPT
table1 but not in table2 using EXCEPT?Solution
Step 1: Identify EXCEPT syntax
The correct syntax is: SELECT ... EXCEPT SELECT ... to get rows in first query not in second.Step 2: Check options for correct usage
SELECT * FROM table1 EXCEPT SELECT * FROM table2; matches correct syntax; others misuse INTERSECT, UNION, or EXCEPT ALL (which is valid but different).Final Answer:
SELECT * FROM table1 EXCEPT SELECT * FROM table2; -> Option BQuick Check:
EXCEPT syntax = SELECT ... EXCEPT SELECT ... [OK]
- Using INTERSECT instead of EXCEPT
- Adding UNION incorrectly
- Using EXCEPT ALL when not needed
table_a:id
1
2
3
table_b:id
2
3
4
What is the result of:
SELECT id FROM table_a INTERSECT SELECT id FROM table_b;Solution
Step 1: List rows from both tables
table_a has ids 1, 2, 3; table_b has ids 2, 3, 4.Step 2: Find common ids using INTERSECT
Common ids are 2 and 3 only.Final Answer:
[2, 3] -> Option CQuick Check:
INTERSECT = common rows = [2, 3] [OK]
- Including ids only in one table
- Confusing INTERSECT with UNION
- Expecting all ids combined
SELECT id, name FROM table1 EXCEPT SELECT id FROM table2;
Solution
Step 1: Check column counts in both SELECTs
First SELECT returns two columns (id, name), second returns one column (id).Step 2: Understand EXCEPT requirements
EXCEPT requires both SELECTs to have the same number and compatible types of columns.Final Answer:
The number of columns in both SELECTs differ -> Option AQuick Check:
EXCEPT needs equal columns [OK]
- Ignoring column count mismatch
- Thinking EXCEPT needs WHERE clause
- Assuming table names cause error
employees with columns id, namecontractors with columns id, nameYou want to find people who are either employees or contractors but not both.
Which query correctly returns this list?
Solution
Step 1: Understand the requirement
We want people in employees or contractors but not in both (symmetric difference).Step 2: Use EXCEPT and UNION to find symmetric difference
EXCEPT finds those in one table but not the other; UNION combines both exclusive sets.Step 3: Analyze options
SELECT id, name FROM employees EXCEPT SELECT id, name FROM contractors UNION SELECT id, name FROM contractors EXCEPT SELECT id, name FROM employees; correctly combines EXCEPT in both directions with UNION to get symmetric difference.Final Answer:
SELECT id, name FROM employees EXCEPT SELECT id, name FROM contractors UNION SELECT id, name FROM contractors EXCEPT SELECT id, name FROM employees; -> Option AQuick Check:
Symmetric difference = EXCEPT + UNION [OK]
- Using only INTERSECT or UNION
- Using EXCEPT one way only
- Confusing symmetric difference with intersection
Ready to prove it?
Pass the test and this pattern moves unknown
