Full answer
A bag of product names from all order lines, minus a bag from cancelled orders:
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);PostgreSQL:
sql
SELECT product FROM order_items
EXCEPT ALL
SELECT oi.product FROM order_items oi JOIN orders o ON o.id = oi.order_id WHERE o.status = 'cancelled'
ORDER BY product;| product |
|---|
| Bag |
| Bag |
| Lamp |
| Notebook |
| Notebook |
| Pen |
| Pen |
Pen appears in three order lines and in one cancelled line, so EXCEPT ALL leaves two Pens. Plain EXCEPT would remove Pen completely, because it works on distinct rows.
SQLite rejects the ALL form:
sql
SELECT product FROM order_items
EXCEPT ALL
SELECT product FROM order_items WHERE order_id = 103;
-- error on purposetext
Error: near "ALL": syntax errorTo emulate EXCEPT ALL on SQLite, count each value on both sides and subtract:
sql
WITH a AS (SELECT product, COUNT(*) AS n FROM order_items GROUP BY product),
b AS (SELECT oi.product, COUNT(*) AS n FROM order_items oi
JOIN orders o ON o.id = oi.order_id WHERE o.status = 'cancelled' GROUP BY oi.product)
SELECT a.product, a.n - COALESCE(b.n, 0) AS remaining
FROM a LEFT JOIN b ON b.product = a.product
WHERE a.n - COALESCE(b.n, 0) > 0
ORDER BY a.product;| product | remaining |
|---|---|
| Bag | 2 |
| Lamp | 1 |
| Notebook | 2 |
| Pen | 2 |
sql
CREATE TABLE customers (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
country TEXT
);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER REFERENCES customers(id),
order_date TEXT NOT NULL,
status TEXT NOT NULL
);
CREATE TABLE order_items (
order_id INTEGER REFERENCES orders(id),
product TEXT NOT NULL,
qty INTEGER NOT NULL,
unit_price INTEGER NOT NULL
);
INSERT INTO customers VALUES (1,'Ana','Spain'),(2,'Bilal','UAE'),(3,'Chloe','France'),(4,'Dev','India');
INSERT INTO orders VALUES
(101,1,'2026-01-05','shipped'),(102,1,'2026-02-11','shipped'),
(103,2,'2026-02-14','cancelled'),(104,3,'2026-03-01','shipped'),
(105,2,'2026-03-09','pending'),(106,NULL,'2026-03-12','shipped');
INSERT INTO order_items VALUES
(101,'Notebook',2,4),(101,'Pen',5,1),(102,'Bag',1,30),(103,'Pen',10,1),
(104,'Notebook',3,4),(104,'Bag',1,30),(105,'Lamp',1,25),(106,'Pen',2,1);
-- error on purpose
SELECT product FROM order_items
EXCEPT ALL
SELECT product FROM order_items WHERE order_id = 103;
WITH a AS (SELECT product, COUNT(*) AS n FROM order_items GROUP BY product),
b AS (SELECT oi.product, COUNT(*) AS n FROM order_items oi
JOIN orders o ON o.id = oi.order_id WHERE o.status = 'cancelled' GROUP BY oi.product)
SELECT a.product, a.n - COALESCE(b.n, 0) AS remaining
FROM a LEFT JOIN b ON b.product = a.product
WHERE a.n - COALESCE(b.n, 0) > 0
ORDER BY a.product;Checked on SQLite 3.46.1, run twice with identical output. PostgreSQL results in the answer were checked on PostgreSQL 17.12.
Key points
- EXCEPT ALL and INTERSECT ALL respect duplicate counts
- EXCEPT ALL subtracts counts; INTERSECT ALL takes the minimum count
- PostgreSQL supports both; SQLite does not
- Counting and subtracting emulates them
Common mistakes
- Assuming EXCEPT removes only one copy of a duplicate
- Writing EXCEPT ALL in SQLite and getting a syntax error
Follow-ups
- How would you emulate INTERSECT ALL?
- When would bag semantics matter in a reconciliation report?
Tips
- Use a small count example to show the arithmetic