Before you start, you need
Combine lists that represent the same thing
You have one list of customers with pending orders and another with shipped orders. You want to combine them into one. UNION stacks the rows of one list with those of the other; it does not look for matching pairs like a JOIN.
The queries must return the same number of columns in compatible positions. In this case both return customer_id: the values represent the same thing. Mixing cities and departments in one column would not produce a useful list.
UNION removes repeated rows. A customer who is in both lists appears once. UNION ALL keeps every occurrence, including several orders from the same customer.
Choose according to the question: a list of distinct customers, or every occurrence. The column headings come from the first query, and an ORDER BY at the end sorts the whole list.
SELECT column_name FROM ...
UNION
SELECT column_name FROM ...;See it in action
Which customers have pending or shipped orders?
SELECT customer_id
FROM orders
WHERE status = 'pending'
UNION
SELECT customer_id
FROM orders
WHERE status = 'shipped'
ORDER BY customer_id;| customer_id |
|---|
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 7 |
| 8 |
| 9 |
- Each branch produces customer ids. They have one column and the same meaning.
- UNION combines the lists and keeps each id only once, even if a customer has several orders.
- UNION ALL keeps every occurrence. It is useful when the question needs to represent each order or each source.
- The final ORDER BY sorts the combined result. The column names come from the first branch.
Practice with help
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Combine customers from two lists
Use UNION to combine the customer_id values from pending and shipped orders. Return customer_id without repeats and sort by customer_id.
Expected result: 10 rows with the column customer_id, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
Both branches must produce customer ids; decide whether they should repeat.
Filter pending orders in one branch and shipped orders in the other.
UNION deduplicates; add a single ORDER BY at the end.
Solution explained
SELECT customer_id FROM orders WHERE status = 'pending' UNION SELECT customer_id FROM orders WHERE status = 'shipped' ORDER BY customer_id;Both lists contain customer ids. UNION keeps each customer once, even if they have several orders.
Your turn
Solve this variation. Spot the part that has changed before you look for help.
Practice
Keep every occurrence
Combine customer_id from pending and shipped orders with UNION ALL. Return customer_id and sort by it.
Expected result: 15 rows with the column customer_id, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
Both branches must describe the same attribute.
Choose the operator that keeps repeats.
The final ORDER BY goes after the second branch.
Solution explained
SELECT customer_id FROM orders WHERE status = 'pending' UNION ALL SELECT customer_id FROM orders WHERE status = 'shipped' ORDER BY customer_id;Each order contributes one occurrence of its customer. UNION ALL does not deduplicate people.
Key takeaways
- The branches need the same number of columns and a compatible meaning.
- UNION removes repeats.
- UNION ALL keeps every occurrence.
- The final ORDER BY goes after the branches.