UNION combining result sets in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When we combine two lists of data using UNION, we want to know how the work grows as the lists get bigger.
How does the time to get the combined list change when the input lists grow?
Analyze the time complexity of the following code snippet.
SELECT column1 FROM tableA
UNION
SELECT column1 FROM tableB;
This code combines unique values from two tables into one list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each row in both tables and comparing to remove duplicates.
- How many times: Each row in tableA and tableB is checked once, then comparisons happen to find unique rows.
As the number of rows in both tables 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 combine grows roughly in proportion to n log n, where n is the total number of rows.
[X] Wrong: "UNION runs in constant time no matter how big the tables are."
[OK] Correct: The database must look at every row to find unique values, so more rows mean more work.
Understanding how combining data grows with size helps you explain query performance clearly and confidently.
"What if we used UNION ALL instead of UNION? How would the time complexity change?"
Practice
UNION operator do when combining results from two SELECT queries?Solution
Step 1: Understand UNION behavior
The UNION operator combines rows from two or more SELECT queries into a single result set.Step 2: Check duplicate handling
By default, UNION removes duplicate rows to ensure unique results.Final Answer:
Combines rows from both queries and removes duplicate rows -> Option BQuick Check:
UNION removes duplicates = A [OK]
- Confusing UNION with UNION ALL
- Thinking UNION joins tables by columns
- Assuming UNION filters rows
Solution
Step 1: Review correct UNION syntax
The correct syntax is to write two SELECT statements separated by the UNION keyword.Step 2: Identify invalid options
Options B, C, and D misuse JOIN or WHERE with UNION, which is incorrect syntax.Final Answer:
SELECT col1 FROM table1 UNION SELECT col1 FROM table2; -> Option DQuick Check:
UNION syntax = SELECT ... UNION SELECT ... [OK]
- Using JOIN instead of UNION
- Placing WHERE before UNION
- Combining UNION ALL with JOIN incorrectly
Table A: id
1
2
3Table B: id
2
3
4What is the result of:
SELECT id FROM A UNION SELECT id FROM 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: Apply UNION behavior
UNION combines all rows and removes duplicates, so final list is 1, 2, 3, 4.Final Answer:
[1, 2, 3, 4] -> Option AQuick Check:
UNION removes duplicates = [1, 2, 3, 4] [OK]
- Expecting duplicates to appear
- Confusing UNION with UNION ALL
- Listing only common ids
SELECT name FROM employees UNION SELECT name, department FROM managers;What is the issue with this query?
Solution
Step 1: Check column counts in SELECTs
The first SELECT returns 1 column (name), the second returns 2 columns (name, department).Step 2: Understand UNION column rules
UNION requires all SELECT statements to have the same number of columns with compatible types.Final Answer:
The number of columns in both SELECT statements differ -> Option AQuick Check:
UNION needs same columns = C [OK]
- Ignoring column count mismatch
- Thinking UNION filters duplicates only
- Assuming WHERE clause is mandatory
Sales2023(product, amount)
Apple, 100
Banana, 150Sales2024(product, amount)
Banana, 200
Cherry, 300Write a query using UNION to list all unique products sold in both years, sorted alphabetically.
Solution
Step 1: Select product column from both tables
We want unique products, so select only the product column from both tables.Step 2: Use UNION to combine and remove duplicates
UNION combines both lists and removes duplicates, giving unique products.Step 3: Sort results alphabetically
ORDER BY product sorts the final list alphabetically.Final Answer:
SELECT product FROM Sales2023 UNION SELECT product FROM Sales2024 ORDER BY product; -> Option CQuick Check:
UNION + ORDER BY product = B [OK]
- Using UNION ALL which keeps duplicates
- Selecting amount column when only product needed
- Using JOIN instead of UNION
