Rows in one query but not another (EXCEPT)
A minus B is one line with EXCEPT; the operand order is the whole meaning.
The task
You want the customers whose orders are all still pending: customers with pending orders, minus customers who have had anything shipped.
The snippet
Runs unchanged on current versions of SQL Server, PostgreSQL, MySQL, SQLite and Oracle.
SELECT customer_id
FROM orders
WHERE status = 'pending'
EXCEPT
SELECT customer_id
FROM orders
WHERE status = 'shipped'
ORDER BY customer_id;
Cómo funciona
EXCEPT keeps the rows of the first query that do not appear in the second, deduplicated. Swapping the operands answers the opposite question, so read it as A minus B. Oracle accepts EXCEPT from 21c and has always had MINUS with the same meaning; MySQL supports EXCEPT from 8.0.31, and older versions can use the NOT EXISTS anti-join below.
Variations
Anti-join emulation
SELECT DISTINCT a.customer_id
FROM orders a
WHERE a.status = 'pending'
AND NOT EXISTS (
SELECT 1 FROM orders b
WHERE b.customer_id = a.customer_id
AND b.status = 'shipped'
);
Questions that come up
Why dedupe in the emulation but not need to in EXCEPT?
EXCEPT removes duplicates by definition. In the NOT EXISTS version the same customer appears once per pending order, so DISTINCT restores the set semantics.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.