What if you could combine multiple lists into one with a single simple command?
Why UNION combining result sets in SQL? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have two separate lists of customer orders from two different stores saved in separate files. You want to see all orders together in one list to understand total sales.
Manually copying and pasting orders from one list to another is slow and mistakes happen easily. You might miss some orders or duplicate others, making your final list unreliable.
The UNION command in SQL lets you combine these two lists into one clean, unified list automatically. It saves time and ensures no duplicates sneak in unless you want them.
Copy orders from Store1 list Paste into Master list Copy orders from Store2 list Paste into Master list
SELECT * FROM Store1Orders UNION SELECT * FROM Store2Orders
With UNION, you can easily merge multiple sets of data into one complete view for better decisions.
A business owner combines sales data from multiple branches to see total revenue without errors or extra work.
Manually merging data is slow and error-prone.
UNION combines multiple result sets into one automatically.
This makes data analysis faster and more reliable.
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
