Both SELECT branches must return the same number of columns in the same order. Put ORDER BY after the final branch if the combined list needs sorting.
Use UNION when duplicate rows are unwanted. Use UNION ALL when they are meaningful or when each branch already represents a distinct category.
Worked example
Make one outreach list of business customers and German customers.
SELECT name, email FROM customers WHERE segment = 'business' UNION SELECT name, email FROM customers WHERE country = 'Germany' ORDER BY name;
| name | |
|---|---|
| Anna Schmidt | ANNA.SCHMIDT@example.de |
| Anna Schmidt | anna.schmidt@example.de |
| Ciara Walsh | ciara.walsh@example.ie |
| Elena García | elena.garcia@example.es |
| Emma de Vries | emma.devries@example.nl |
| Giorgos Ioannou | giorgos.ioannou@example.com |
| Jonas Becker | jonas.becker@example.com |
| Marco Bianchi | marco.bianchi@example.it |
| Mia Weber | mia.weber@example.de |
| Sean Byrne | sean.byrne@example.ie |
Common mistakes
- Using UNION ALL for overlapping lists and contacting a person twice.
- Trying to combine branches with different column counts.