In both, or only in one: INTERSECT and EXCEPT
Two more ways to combine lists compare them instead of stacking them:
INTERSECTkeeps rows that appear in both results.EXCEPTkeeps rows from the first result that aren't in the second.
-- Customers who have ordered online AND by phone
SELECT customer_id FROM orders WHERE channel = 'Online'
INTERSECT
SELECT customer_id FROM orders WHERE channel = 'Phone'-- Customers who ordered in 2025 but not in 2026
SELECT customer_id FROM orders WHERE order_date >= '2025-01-01' AND order_date < '2026-01-01'
EXCEPT
SELECT customer_id FROM orders WHERE order_date >= '2026-01-01'Both remove duplicates, like UNION does. Wrap one in brackets and SELECT COUNT(*) FROM (…) to
count the result.
Other databases:
EXCEPTis calledMINUSin Oracle. MySQL only addedINTERSECTandEXCEPTin version 8.0.31; on older versions, useEXISTSandNOT EXISTS.