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);
✓ 3 rows changed

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;
namephonekind
Anan WongsaNULLcustomer
City Flour02-333-4444supplier
Kanya Boonmee084-246-1357customer
Lalita Meesuk087-654-3210customer
Malee Srisuk089-111-2222customer
Narin Thongdee082-777-8888customer
Northern Beans053-111-222supplier
Pim Chaiya086-555-0101customer
Siriporn KaewdeeNULLcustomer
Somchai Jaidee081-234-5678customer
Southern NutsNULLsupplier
Tawan Rungruang085-123-4567customer
Wichai Saelim083-999-0000customer
13 rows

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 rows

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
13 rows

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;
Error: SELECTs to the left and right of UNION do not have the same number of result columns

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;
contactcity
Anan WongsaBangkok
City FlourBangkok
Lalita MeesukBangkok
Somchai JaideeBangkok
Wichai SaelimBangkok
Kanya BoonmeeChiang Mai
Malee SrisukChiang Mai
Northern BeansChiang Mai
Southern NutsHat Yai
Narin ThongdeeKhon Kaen
Tawan RungruangKhon Kaen
Pim ChaiyaPhuket
Siriporn KaewdeePhuket
13 rows

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;
Error: ORDER BY clause should come after UNION not before

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:

WordGives
UNIONrows in either side
INTERSECTrows in both sides
EXCEPTrows 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
2 rows

Cities with customers but no supplier:

SELECT city FROM customer
EXCEPT
SELECT city FROM supplier;
city
Khon Kaen
Phuket
2 rows

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

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
);
idnamestock
3Orange Juice0
1 row

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
2 rows

Numbers 9 and 10: the same two customers chapter 12 found.

  1. Every city of the customers and the suppliers, each once, in alphabetical order.
  2. A phone book: the names and phone numbers of customers and suppliers, only those with a number, by name.
  3. 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
5 rows
namephone
City Flour02-333-4444
Kanya Boonmee084-246-1357
Lalita Meesuk087-654-3210
Malee Srisuk089-111-2222
Narin Thongdee082-777-8888
Northern Beans053-111-222
Pim Chaiya086-555-0101
Somchai Jaidee081-234-5678
Tawan Rungruang085-123-4567
Wichai Saelim083-999-0000
10 rows
customer_id
4
6
7
3 rows

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