UNION combines rows and removes repeats
UNION stacks the rows of one SELECT underneath the rows of another. Both SELECT statements must return the same number of columns, in a matching order and of compatible types. UNION then removes any repeated rows, keeping one of each — UNION ALL is the version that keeps every repeat. That tidying takes extra work, so UNION ALL is the faster habit when repeats are fine.
A tiny example
In plain English: Show me every city that customers or suppliers come from, each city once.
SELECT city
FROM customers
UNION
SELECT city
FROM suppliers;Query result
| city |
|---|
| Lisbon |
| Oslo |
| Porto |
Customers and suppliers share some cities, but UNION shows each city only once.
Related SQL lessons
Ready for a break?
A fresh, bite-sized SQL puzzle waits every morning. Tap tiles, see real results. No terminal, no setup, no timers.
Try today's free SQL puzzle