SQL UNION
UNION stacks the results of two or more SELECT statements on top of each other into one result set.
SELECT columns FROM table1
UNION
SELECT columns FROM table2;Rules:
- Every
SELECTmust return the same number of columns. - Corresponding columns should have compatible types.
- Column names come from the first
SELECT. ORDER BYis written once, at the very end, and sorts the combined result.
Cities of customers and suppliers
SELECT city, country FROM customers WHERE country IN ('UK', 'Norway', 'USA')
UNION
SELECT city, country FROM suppliers WHERE country IN ('UK', 'Norway', 'USA')
ORDER BY country, city;city | country
----------+--------
Oslo | Norway
Edinburgh | UK
London | UK
Austin | USA
New York | USA
(5 rows)
Oslo and Austin appear in both tables but only once in the result: UNION removes duplicate rows.
UNION ALL keeps duplicates
UNION ALL simply appends the rows. It is faster, because the database does not need to look for duplicates:
SELECT city FROM customers WHERE country IN ('Norway', 'USA')
UNION ALL
SELECT city FROM suppliers WHERE country IN ('Norway', 'USA')
ORDER BY city;city
--------
Austin
Austin
New York
Oslo
Oslo
(5 rows)
Tip: Use
UNION ALLunless you really want duplicates removed. It is cheaper and does not surprise you by merging rows that just happen to be equal.
Labelling the source
Add a constant column to tell where each row came from:
SELECT 'customer' AS type, name, country FROM customers WHERE country = 'Japan'
UNION ALL
SELECT 'supplier', name, country FROM suppliers WHERE country = 'Japan'
UNION ALL
SELECT 'employee', first_name || ' ' || last_name, NULL FROM employees WHERE employee_id = 8
ORDER BY type, name;type | name | country
---------+---------------+--------
customer | Code & Coffee | Japan
employee | Hana Sato | NULL
supplier | Volt Gadgets | Japan
(3 rows)
Building a small report
UNION ALL can add a total row under a grouped result:
SELECT status, COUNT(*) AS orders, 1 AS sort_key
FROM orders
GROUP BY status
UNION ALL
SELECT 'TOTAL', COUNT(*), 2
FROM orders
ORDER BY sort_key, status;status | orders | sort_key
-----------+--------+---------
cancelled | 7 | 1
delivered | 16 | 1
processing | 8 | 1
shipped | 9 | 1
TOTAL | 40 | 2
(5 rows)
INTERSECT and EXCEPT
Two siblings of UNION compare result sets instead of stacking them:
| Operator | Returns |
|---|---|
A UNION B |
rows in A or B (duplicates removed) |
A UNION ALL B |
all rows of A, then all rows of B |
A INTERSECT B |
rows in both A and B |
A EXCEPT B |
rows in A but not in B (Oracle: MINUS) |
SELECT country FROM customers
INTERSECT
SELECT country FROM suppliers
ORDER BY country;country
-------
France
India
Japan
Norway
Spain
UK
USA
(7 rows)
MySQL supports INTERSECT and EXCEPT only since version 8.0.31.
Test yourself
What is the difference between UNION and UNION ALL?
- UNION ALL removes duplicates
- UNION removes duplicates; UNION ALL keeps them
- UNION only works on two tables
- There is none
Where do the column names of a UNION result come from?
- The last SELECT
- The first SELECT
- Both are merged
- They are numbered
Which operator returns countries that have customers but no suppliers?
- UNION
- INTERSECT
- EXCEPT
- UNION ALL