Why set operations matter in PostgreSQL - Performance Analysis
When working with databases, set operations like UNION or INTERSECT combine or compare groups of rows.
We want to know how the time to run these operations changes as the data grows.
Analyze the time complexity of the following SQL using UNION.
SELECT id FROM employees
UNION
SELECT id FROM managers;
This code combines two lists of IDs, removing duplicates to get unique IDs from both tables.
Look for repeated work done by the database engine.
- Primary operation: Scanning each table's rows to collect IDs.
- How many times: Once per table, then comparing all collected rows to remove duplicates.
As the number of rows in each table grows, the work to scan and compare grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 scans and comparisons |
| 100 | About 200 scans and comparisons |
| 1000 | About 2000 scans and comparisons |
Pattern observation: The work grows roughly in direct proportion to the total number of rows combined.
Time Complexity: O(n log n)
This means the time to run the UNION grows roughly in proportion to n log n as the total number of rows increases.
[X] Wrong: "UNION runs instantly no matter how big the tables are."
[OK] Correct: The database must look at every row to combine and remove duplicates, so bigger tables take more time.
Understanding how set operations scale helps you write efficient queries and explain your choices clearly.
"What if we used UNION ALL instead of UNION? How would the time complexity change?"
Practice
UNION and INTERSECT in PostgreSQL?Solution
Step 1: Understand set operations
Set operations like UNION and INTERSECT combine or compare results from multiple SELECT queries.Step 2: Identify their purpose
They help find unique, common, or different data across queries without modifying tables.Final Answer:
To combine or compare results from multiple SELECT queries -> Option DQuick Check:
Set operations = combine/compare SELECT results [OK]
- Thinking set operations modify tables
- Confusing set operations with DML commands
- Assuming set operations create new tables
Solution
Step 1: Recall UNION syntax
The correct syntax uses UNION keyword between two SELECT statements with matching columns.Step 2: Check options
OnlySELECT col1 FROM table1 UNION SELECT col1 FROM table2;is correct. JOIN is for table joins;+is invalid syntax; INTERSECT is a different operation.Final Answer:
SELECT col1 FROM table1 UNION SELECT col1 FROM table2; -> Option BQuick Check:
UNION syntax = SELECT ... UNION SELECT ... [OK]
- Using + instead of UNION
- Confusing JOIN with UNION
- Mixing INTERSECT with UNION syntax
table1: (id) values (1), (2), (3);table2: (id) values (2), (3), (4);What is the result of this query?
SELECT id FROM table1 INTERSECT SELECT id FROM table2;
Solution
Step 1: Understand INTERSECT operation
INTERSECT returns only values present in both queries.Step 2: Compare table1 and table2 ids
table1 has 1,2,3; table2 has 2,3,4; common values are 2 and 3.Final Answer:
[2, 3] -> Option AQuick Check:
INTERSECT = common values [OK]
- Including all values from both tables
- Confusing INTERSECT with UNION
- Returning values only from one table
SELECT id FROM table1 UNION ALL SELECT id FROM table2;
It returns duplicate rows. How can you fix it to return only unique rows?
Solution
Step 1: Understand UNION ALL vs UNION
UNION ALL returns all rows including duplicates; UNION removes duplicates.Step 2: Fix query to remove duplicates
Replacing UNION ALL with UNION removes duplicates automatically.Final Answer:
Replace UNION ALL with UNION -> Option AQuick Check:
UNION removes duplicates, UNION ALL does not [OK]
- Using UNION ALL when duplicates are unwanted
- Adding DISTINCT inside each SELECT unnecessarily
- Using INTERSECT which returns only common rows
purchases_2022(customer_id) and purchases_2023(customer_id).Which query correctly returns this list without duplicates?
Solution
Step 1: Understand the goal
We want all unique customer IDs from either year, combined without duplicates.Step 2: Evaluate options
SELECT customer_id FROM purchases_2022 INTERSECT SELECT customer_id FROM purchases_2023; returns only customers in both years (common). SELECT customer_id FROM purchases_2022 UNION SELECT customer_id FROM purchases_2023; returns all unique customers from both years. SELECT customer_id FROM purchases_2022 UNION ALL SELECT customer_id FROM purchases_2023; returns all including duplicates. SELECT DISTINCT customer_id FROM purchases_2022, purchases_2023; creates a cross join, not union.Final Answer:
SELECT customer_id FROM purchases_2022 UNION SELECT customer_id FROM purchases_2023; -> Option CQuick Check:
UNION = unique combined rows [OK]
- Using INTERSECT to get all customers
- Using UNION ALL causing duplicates
- Using cross join instead of union
Ready to prove it?
Pass the test and this pattern moves unknown
