Why combining result sets is useful in MySQL - Performance Analysis
When we combine result sets in SQL, we want to see how the time to get results changes as data grows.
We ask: How does combining tables affect the work the database does?
Analyze the time complexity of this SQL combining two result sets.
SELECT name FROM employees
UNION
SELECT name FROM customers;
This code gets names from two tables and combines them into one list without duplicates.
Look at what repeats when combining results.
- Primary operation: Scanning each table's rows to collect names.
- How many times: Once for each table, then a step to remove duplicates.
As the number of rows in each table grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 rows scanned plus duplicate checks |
| 100 | About 200 rows scanned plus duplicate checks |
| 1000 | About 2000 rows scanned plus duplicate checks |
Pattern observation: The work grows roughly in direct proportion to the total rows combined.
Time Complexity: O(n)
This means the time to combine results grows roughly in a straight line with the total number of rows.
[X] Wrong: "Combining two tables always doubles the time exactly."
[OK] Correct: The time depends on total rows, but removing duplicates can add extra work, so it's not just doubling.
Understanding how combining results affects time helps you explain database behavior clearly and confidently.
"What if we used UNION ALL instead of UNION? How would the time complexity change?"
Practice
UNION or UNION ALL useful in MySQL?Solution
Step 1: Understand the purpose of combining result sets
Combining result sets lets you merge rows from different tables or queries into one output.Step 2: Identify the benefit of combined data
This helps to view or report data together without changing the original tables.Final Answer:
It allows you to see data from multiple tables together in one list. -> Option AQuick Check:
Combining result sets = viewing merged data [OK]
- Thinking UNION speeds up queries automatically
- Believing UNION deletes data permanently
- Confusing combining with creating new tables
Solution
Step 1: Recall syntax for combining queries without duplicates
TheUNIONkeyword combines results and removes duplicates.Step 2: Identify correct syntax
The correct syntax isSELECT ... UNION SELECT ....UNION ALLkeeps duplicates, JOIN and AND are incorrect here.Final Answer:
SELECT * FROM table1 UNION SELECT * FROM table2; -> Option AQuick Check:
UNION = combine without duplicates [OK]
- Using JOIN instead of UNION to combine result sets
- Confusing UNION ALL with UNION
- Using AND keyword incorrectly
table1 has rows: (1, 'Apple'), (2, 'Banana')table2 has rows: (2, 'Banana'), (3, 'Cherry')What is the result of this query?
SELECT id, name FROM table1 UNION SELECT id, name FROM table2;
Solution
Step 1: Understand UNION removes duplicates
The row (2, 'Banana') appears in both tables but will appear only once in the result.Step 2: Combine unique rows from both tables
Rows (1, 'Apple') and (3, 'Cherry') are unique and included as is.Final Answer:
[(1, 'Apple'), (2, 'Banana'), (3, 'Cherry')] -> Option CQuick Check:
UNION removes duplicates, so only one (2, 'Banana') [OK]
- Expecting duplicate rows to appear twice
- Confusing UNION with UNION ALL output
- Ignoring that UNION sorts and removes duplicates
SELECT * FROM table1 UNION SELECT * FROM table2;
Solution
Step 1: Understand UNION behavior
UNION removes duplicate rows from combined results.Step 2: Identify correct way to keep duplicates
To keep duplicates, use UNION ALL instead of UNION.Final Answer:
It will remove duplicate rows, so duplicates are lost. -> Option DQuick Check:
UNION removes duplicates, use UNION ALL to keep them [OK]
- Thinking UNION keeps duplicates
- Believing UNION requires ORDER BY
- Confusing UNION with JOIN behavior
sales_2023 and sales_2024. Both have columns product_id and units_sold. You want a report showing all sales from both years including duplicates, but also want to sort by units_sold descending. Which query correctly achieves this?Solution
Step 1: Choose UNION ALL to keep duplicates
Since duplicates must be included, UNION ALL is needed.Step 2: Apply ORDER BY after the combined query
ORDER BY must be applied to the entire combined result, so it goes after both SELECTs.Step 3: Use column position or name in ORDER BY
UsingORDER BY 2 DESCsorts by the second columnunits_solddescending correctly.Final Answer:
SELECT product_id, units_sold FROM sales_2023 UNION ALL SELECT product_id, units_sold FROM sales_2024 ORDER BY 2 DESC; -> Option BQuick Check:
UNION ALL + ORDER BY after combined query = correct [OK]
- Using UNION instead of UNION ALL loses duplicates
- Placing ORDER BY inside first SELECT only
- Not sorting combined result properly
Ready to prove it?
Pass the test and this pattern moves unknown
