UNION combining result sets in SQL - Time & Space Complexity
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?"