14. UNION: putting SELECTs together
Stack the results of several SELECTs, find what they share, or what one has and the other hasn't.
A table of suppliers
This chapter needs data from two places that look alike. Our shop buys from 3 suppliers; make a table for them (making tables is in chapters 16 and 20; just run it for now):
CREATE TABLE supplier (id INTEGER PRIMARY KEY, name TEXT NOT NULL, city TEXT, phone TEXT);
INSERT INTO supplier VALUES
(1, 'Northern Beans', 'Chiang Mai', '053-111-222'),
(2, 'City Flour', 'Bangkok', '02-333-4444'),
(3, 'Southern Nuts', 'Hat Yai', NULL);UNION ALL: one result from several
JOIN puts tables side by side (more columns); UNION puts results one under another (more rows). A contact list of customers and suppliers together:
SELECT name, phone, 'customer' AS kind FROM customer
UNION ALL
SELECT name, phone, 'supplier' FROM supplier
ORDER BY name;| name | phone | kind |
|---|---|---|
| Anan Wongsa | NULL | customer |
| City Flour | 02-333-4444 | supplier |
| Kanya Boonmee | 084-246-1357 | customer |
| Lalita Meesuk | 087-654-3210 | customer |
| Malee Srisuk | 089-111-2222 | customer |
| Narin Thongdee | 082-777-8888 | customer |
| Northern Beans | 053-111-222 | supplier |
| Pim Chaiya | 086-555-0101 | customer |
| Siriporn Kaewdee | NULL | customer |
| Somchai Jaidee | 081-234-5678 | customer |
| Southern Nuts | NULL | supplier |
| Tawan Rungruang | 085-123-4567 | customer |
| Wichai Saelim | 083-999-0000 | customer |
13 rows: 10 customers and 3 suppliers. The kind column is fixed text we put in ourselves, to show where each row came from.
UNION and UNION ALL
UNION ALL just stacks every row. UNION (without ALL) also drops repeated rows, as DISTINCT does. The cities of both tables:
SELECT city FROM customer
UNION
SELECT city FROM supplier;| city |
|---|
| Bangkok |
| Chiang Mai |
| Hat Yai |
| Khon Kaen |
| Phuket |
5 different cities. With UNION ALL you get all 13 rows, Bangkok 5 times (4 customers, 1 supplier):
SELECT city FROM customer
UNION ALL
SELECT city FROM supplier;| city |
|---|
| Bangkok |
| Chiang Mai |
| Bangkok |
| Phuket |
| Khon Kaen |
| Chiang Mai |
| Bangkok |
| Phuket |
| Khon Kaen |
| Bangkok |
| Chiang Mai |
| Bangkok |
| Hat Yai |
When you know there are no repeats, or you want to keep them, use UNION ALL: it is faster, since the database needn't look for repeats. Use UNION only when you really want them gone.
The rules of putting results together
1. The same number of columns. Every SELECT must give the same number of columns, or the database can't tell how to stack them:
SELECT name, phone FROM customer
UNION
SELECT name FROM supplier;Columns stack by position, not by name: column 1 goes under column 1. Swap two by mistake and names end up among the phone numbers, with no error to warn you.
2. The names come from the first SELECT. Name a column with AS in the first SELECT, and ORDER BY uses that name:
SELECT name AS contact, city FROM customer
UNION ALL
SELECT name, city FROM supplier
ORDER BY city, contact;| contact | city |
|---|---|
| Anan Wongsa | Bangkok |
| City Flour | Bangkok |
| Lalita Meesuk | Bangkok |
| Somchai Jaidee | Bangkok |
| Wichai Saelim | Bangkok |
| Kanya Boonmee | Chiang Mai |
| Malee Srisuk | Chiang Mai |
| Northern Beans | Chiang Mai |
| Southern Nuts | Hat Yai |
| Narin Thongdee | Khon Kaen |
| Tawan Rungruang | Khon Kaen |
| Pim Chaiya | Phuket |
| Siriporn Kaewdee | Phuket |
3. ORDER BY and LIMIT once, at the very end. They sort the whole stacked result. An ORDER BY after the first SELECT (before UNION) is refused. This is wrong:
SELECT city FROM customer ORDER BY city -- wrong: ORDER BY before UNION
UNION
SELECT city FROM supplier;In ORDER BY, use the column names of the first SELECT (or column numbers, such as ORDER BY 1). SQLite and Phai SQL accept names from the other SELECTs as well, but many other databases don't. Written this way, it works everywhere.
INTERSECT and EXCEPT
Besides stacking, two SELECTs can be compared two more ways:
| Word | Gives |
|---|---|
UNION | rows in either side |
INTERSECT | rows in both sides |
EXCEPT | rows in the first side but not the second |
Cities with both customers and suppliers:
SELECT city FROM customer
INTERSECT
SELECT city FROM supplier;| city |
|---|
| Bangkok |
| Chiang Mai |
Cities with customers but no supplier:
SELECT city FROM customer
EXCEPT
SELECT city FROM supplier;| city |
|---|
| Khon Kaen |
| Phuket |
INTERSECT and EXCEPT drop repeats, as UNION does. Swap the sides of EXCEPT and the result changes: try it, and you get Hat Yai, a city with a supplier but no customer.
In real work: products to look after
The shop wants to know two things. First, products in stock that have never sold (dead stock):
SELECT id FROM product WHERE stock > 0
EXCEPT
SELECT product_id FROM order_item;| id |
|---|
No rows, which is good news: every product in stock has sold at least once. An empty result is an answer too.
Second, products that have sold but are out of stock now (time to reorder), with their names through IN from chapter 13:
SELECT id, name, stock FROM product
WHERE id IN (
SELECT product_id FROM order_item
EXCEPT
SELECT id FROM product WHERE stock > 0
);| id | name | stock |
|---|---|---|
| 3 | Orange Juice | 0 |
Orange Juice has sold, but 0 are left. (Highlighter is out of stock too, but it never sold, so it isn't on the list.)
The customers who joined but never ordered can be written with EXCEPT as well, shorter than chapter 12's LEFT JOIN:
SELECT id FROM customer
EXCEPT
SELECT customer_id FROM orders;| id |
|---|
| 9 |
| 10 |
Numbers 9 and 10: the same two customers chapter 12 found.
- Every city of the customers and the suppliers, each once, in alphabetical order.
- A phone book: the names and phone numbers of customers and suppliers, only those with a number, by name.
- Which customers (
customer_id) ordered both in October and in November 2026?
Show the answer
SELECT city FROM customer UNION SELECT city FROM supplier ORDER BY city;
SELECT name, phone FROM customer WHERE phone IS NOT NULL UNION ALL SELECT name, phone FROM supplier WHERE phone IS NOT NULL ORDER BY name;
SELECT customer_id FROM orders WHERE ordered LIKE '2026-10-%' INTERSECT SELECT customer_id FROM orders WHERE ordered LIKE '2026-11-%';| city |
|---|
| Bangkok |
| Chiang Mai |
| Hat Yai |
| Khon Kaen |
| Phuket |
| name | phone |
|---|---|
| City Flour | 02-333-4444 |
| Kanya Boonmee | 084-246-1357 |
| Lalita Meesuk | 087-654-3210 |
| Malee Srisuk | 089-111-2222 |
| Narin Thongdee | 082-777-8888 |
| Northern Beans | 053-111-222 |
| Pim Chaiya | 086-555-0101 |
| Somchai Jaidee | 081-234-5678 |
| Tawan Rungruang | 085-123-4567 |
| Wichai Saelim | 083-999-0000 |
| customer_id |
|---|
| 4 |
| 6 |
| 7 |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made