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. If two complete rows are identical, UNION keeps just one of them. UNION ALL is the version that keeps every repeat.
A tiny example
In plain English: Show me every city that students or teachers come from, each city once.
SELECT city
FROM students
UNION
SELECT city
FROM teachers;What comes back
| city |
|---|
| Lisbon |
| Oslo |
| Porto |
Students and teachers share some cities, but UNION shows each city only once.
Related SQL lessons
Ready for a break?
Take on today's cozy puzzle. A fresh, bite-sized query waits every morning — no timers, no pressure.
play today's sip