UNION ALL with duplicates in SQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When combining results from two lists in a database, it is important to know how the time to do this grows as the lists get bigger.
We want to understand how the work changes when using UNION ALL, which keeps all duplicates.
Analyze the time complexity of the following code snippet.
SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;
This code combines all rows from two tables into one list, including any duplicates.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading all rows from both tables one by one.
- How many times: Once for each row in each table, no extra checks for duplicates.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 rows in each table | About 20 reads |
| 100 rows in each table | About 200 reads |
| 1000 rows in each table | About 2000 reads |
Pattern observation: The work grows directly with the total number of rows combined.
Time Complexity: O(n + m)
This means the time to run grows in a straight line with the total rows from both tables added together.
[X] Wrong: "UNION ALL removes duplicates so it takes longer to check each row."
[OK] Correct: UNION ALL does not check for duplicates, it simply adds all rows, so it runs faster than UNION which removes duplicates.
Understanding how UNION ALL works helps you explain how combining data sets affects performance, a useful skill when working with databases in real projects.
"What if we changed UNION ALL to UNION? How would the time complexity change?"
Practice
UNION ALL do when combining results from two queries?Solution
Step 1: Understand UNION ALL behavior
UNION ALL combines results from two queries and keeps all rows, including duplicates.Step 2: Compare with UNION
Unlike UNION, UNION ALL does not remove duplicate rows.Final Answer:
It combines all rows from both queries including duplicates. -> Option BQuick Check:
UNION ALL keeps duplicates = A [OK]
- Confusing UNION ALL with UNION
- Thinking duplicates are removed
- Assuming it returns only unique rows
Solution
Step 1: Check correct UNION ALL syntax
The correct syntax is to write two SELECT statements separated by 'UNION ALL'.Step 2: Identify syntax errors in options
SELECT col1 FROM table1 UNIONALL SELECT col1 FROM table2; writes UNIONALL as one word (missing space), SELECT col1 FROM table1 UNION ALL FROM table2; misses the second SELECT keyword, SELECT col1 FROM table1 UNION ALL SELECT FROM table2; misses columns after the second SELECT. SELECT col1 FROM table1 UNION ALL SELECT col1 FROM table2; uses correct syntax.Final Answer:
SELECT col1 FROM table1 UNION ALL SELECT col1 FROM table2; -> Option CQuick Check:
Correct UNION ALL syntax = B [OK]
- Writing UNIONALL as one word
- Missing SELECT in second query
- Incorrect keyword order
Table A: id
1
2
3Table B: id
2
3
4What is the result of:
SELECT id FROM A UNION ALL SELECT id FROM B;Solution
Step 1: List rows from first query
SELECT id FROM A returns [1, 2, 3].Step 2: List rows from second query
SELECT id FROM B returns [2, 3, 4].Step 3: Combine results with UNION ALL
UNION ALL keeps duplicates, so combined list is [1, 2, 3, 2, 3, 4].Final Answer:
[1, 2, 3, 2, 3, 4] -> Option AQuick Check:
UNION ALL keeps duplicates = A [OK]
- Removing duplicates like UNION
- Listing only unique values
- Ignoring order of combined rows
SELECT name FROM employees UNION ALL SELECT name FROM departments;It returns an error. What is the most likely cause?
Solution
Step 1: Check column counts in both SELECTs
UNION ALL requires both SELECTs to have the same number of columns.Step 2: Identify error cause
If employees and departments tables have different columns selected, error occurs.Final Answer:
The two SELECT statements have different numbers of columns. -> Option AQuick Check:
Column count mismatch causes error = C [OK]
- Thinking UNION ALL disallows duplicates only
- Assuming table names must match
- Believing ALL keyword is invalid
Sales_2023: product_id, quantity_sold
Sales_2024: product_id, quantity_soldYou want to create a combined list of all sales including duplicates for analysis. Which query correctly uses UNION ALL and also adds a column
year to identify the source year?Solution
Step 1: Add year column with literal values
Use '2023' and '2024' as string literals to add a year column in each SELECT.Step 2: Ensure both SELECTs have same columns
Both SELECTs must have product_id, quantity_sold, and year columns for UNION ALL.Step 3: Combine with UNION ALL
SELECT product_id, quantity_sold, '2023' AS year FROM Sales_2023 UNION ALL SELECT product_id, quantity_sold, '2024' AS year FROM Sales_2024; correctly combines both tables with year column and keeps duplicates.Final Answer:
SELECT product_id, quantity_sold, '2023' AS year FROM Sales_2023 UNION ALL SELECT product_id, quantity_sold, '2024' AS year FROM Sales_2024; -> Option DQuick Check:
Matching columns with year literals + UNION ALL = D [OK]
- Mismatched columns count in SELECTs
- Using column name 'year' without value
- Forgetting to alias literals as year
