What if you could combine multiple lists perfectly with just one simple command?
Why UNION and UNION ALL in MySQL? - Purpose & Use Cases
Imagine you have two separate lists of customer names from two different stores, and you want to see all customers who visited either store. You try to write down all names by hand or copy-paste them into a single list.
This manual method is slow and messy. You might accidentally write the same name twice or miss some names. It's hard to keep track and update the list when new customers come in.
Using UNION and UNION ALL in SQL lets you combine these lists automatically. UNION removes duplicates, showing each customer once. UNION ALL keeps all entries, including duplicates, so you see every visit.
List1 = ['Alice', 'Bob'] List2 = ['Bob', 'Charlie'] Combined = [] for name in List1: if name not in Combined: Combined.append(name) for name in List2: if name not in Combined: Combined.append(name)
SELECT name FROM store1_customers UNION SELECT name FROM store2_customers
It makes combining data from multiple sources easy, fast, and accurate without manual errors.
A company wants to see all unique email subscribers from two different newsletter lists to send a single campaign without duplicates.
Manual combining is slow and error-prone.
UNION merges lists and removes duplicates automatically.
UNION ALL merges lists and keeps all entries, including duplicates.
Practice
UNION and UNION ALL in MySQL?Solution
Step 1: Understand
UNIONbehaviorUNIONcombines results from two queries and removes duplicate rows to return only unique rows.Step 2: Understand
UNION ALLbehaviorUNION ALLcombines results but keeps all rows, including duplicates, without filtering.Final Answer:
UNIONremoves duplicate rows,UNION ALLkeeps all rows including duplicates. -> Option DQuick Check:
Duplicates removed = UNION, duplicates kept = UNION ALL [OK]
- Thinking UNION ALL removes duplicates
- Confusing sorting behavior with UNION
- Assuming UNION only works on numeric data
UNION ALL in MySQL?Solution
Step 1: Check correct keyword spacing
The correct keyword isUNION ALLwith a space between UNION and ALL.Step 2: Validate SQL syntax
The syntax must be:SELECT ... UNION ALL SELECT ...;without commas or semicolons between the two SELECTs.Final Answer:
SELECT * FROM table1 UNION ALL SELECT * FROM table2; -> Option AQuick Check:
Correct keyword and syntax = SELECT * FROM table1 UNION ALL SELECT * FROM table2; [OK]
- Writing UNIONALL as one word
- Adding commas between SELECT statements
- Using semicolons between SELECTs
Table A:id | name
1 | Alice
2 | Bob
Table B:id | name
2 | Bob
3 | Carol
What will be the result of this query?
SELECT id, name FROM A UNION SELECT id, name FROM B;
Solution
Step 1: Combine rows from both tables
Table A has (1, Alice) and (2, Bob). Table B has (2, Bob) and (3, Carol).Step 2: Apply UNION behavior
UNIONremoves duplicates, so (2, Bob) appears only once in the result.Final Answer:
1 | Alice
2 | Bob
3 | Carol -> Option CQuick Check:
Duplicates removed, unique rows shown [OK]
- Including duplicate rows with UNION
- Confusing UNION with UNION ALL output
- Ignoring duplicate row removal
SELECT id FROM table1
UNION ALL
SELECT id FROM table2 ORDER BY id;
What is wrong with this query?
Solution
Step 1: Understand ORDER BY with UNION ALL
ORDER BY applies to the entire combined result, not to individual SELECTs.Step 2: Identify correct placement
ORDER BY must be placed after the last SELECT and UNION ALL, not inside one SELECT.Final Answer:
ORDER BY must be applied only once at the end of the UNION ALL query. -> Option AQuick Check:
ORDER BY after UNION ALL, not inside SELECT [OK]
- Putting ORDER BY inside one SELECT only
- Trying ORDER BY before UNION ALL
- Assuming UNION ALL disallows ORDER BY
Employees:id | name
1 | John
2 | Jane
3 | Mike
Contractors:id | name
2 | Jane
4 | Anna
You want to list all people from both tables, including duplicates, and sort by name ascending.
Which query achieves this correctly?
Solution
Step 1: Choose UNION ALL to keep duplicates
Since duplicates like Jane (id 2) should appear twice, useUNION ALL.Step 2: Apply ORDER BY after UNION ALL
ORDER BY must come after the full UNION ALL to sort combined results by name ascending.Final Answer:
SELECT id, name FROM Employees UNION ALL SELECT id, name FROM Contractors ORDER BY name ASC; -> Option BQuick Check:
UNION ALL + ORDER BY at end = SELECT id, name FROM Employees UNION ALL SELECT id, name FROM Contractors ORDER BY name ASC; [OK]
- Using UNION instead of UNION ALL (removes duplicates)
- Placing ORDER BY inside one SELECT
- Incorrect syntax with ORDER BY placement
Ready to prove it?
Pass the test and this pattern moves unknown
