Sub topic 12
Double `NOT EXISTS`: “for all” questions
Assume you know SELECT, WHERE, INNER JOIN, LEFT JOIN, aggregate functions, table aliases, and NULL. This chapter builds on correlated subqueries, EXISTS, and NOT EXISTS.
Turning “for all” into “there is no exception”
SQL has no direct FOR ALL operator. To ask whether a customer has ordered every product, rewrite the question:
- Is there a product that this customer has not ordered?
- If no such product exists, the customer ordered every product.
That translation produces two nested NOT EXISTS conditions:
- The outer query chooses a customer.
- The first
NOT EXISTSsearches for a product that violates the requirement. - The inner
NOT EXISTSchecks whether the customer ordered that product.
The customer qualifies only when the first query finds no exception.
A worked example
Suppose these tables exist:
customers(customer_id, name)products(product_id, name)orders(order_id, customer_id)order_items(order_id, product_id)
The following query finds customers who have ordered every product:
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1
FROM products p
WHERE NOT EXISTS (
SELECT 1
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
WHERE o.customer_id = c.customer_id
AND oi.product_id = p.product_id
)
);Read the query from the inside outward. For one customer c and one product p, the innermost query asks whether an order exists for that customer containing that product.
The middle query looks for products for which the answer is no. These are products the customer has not ordered.
The outer NOT EXISTS keeps the customer only when that missing-product query returns no rows. In other words, no exception exists.
The empty-set case
The query also shows an important logical rule. If products is empty, every customer qualifies because there is no product that they failed to order.
This behavior is called vacuous truth. The statement “the customer ordered every product” is true when there are no products to disprove it. That may be logically correct, but it may not match the application rule.
For example:
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
SELECT 1
FROM products p
)
AND NOT EXISTS (
SELECT 1
FROM products p
WHERE NOT EXISTS (
SELECT 1
FROM orders o
JOIN order_items oi ON oi.order_id = o.order_id
WHERE o.customer_id = c.customer_id
AND oi.product_id = p.product_id
)
);Other ways to express the same rule
You can also group matching rows and compare counts:
SELECT c.customer_id, c.name
FROM customers c
JOIN orders o ON o.customer_id = c.customer_id
JOIN order_items oi ON oi.order_id = o.order_id
GROUP BY c.customer_id, c.name
HAVING COUNT(DISTINCT oi.product_id) = (
SELECT COUNT(*)
FROM products
);This version needs more care. It can exclude customers who have no orders, and duplicate or inactive product rows can change the counts. The double NOT EXISTS form states the logic directly and naturally handles customers with no matching rows.
A join-based rewrite can also create duplicate customer rows unless you use grouping or DISTINCT. Choose the form that makes the business rule easiest to review. Then compare execution plans if the query runs over large tables.
Correlation and indexes
Each nested query is correlated:
- The product check refers to
c.customer_id. - The order-item check refers to both
c.customer_idandp.product_id.
Useful indexes often include:
CREATE INDEX idx_orders_customer_id
ON orders (customer_id);
CREATE INDEX idx_order_items_order_product
ON order_items (order_id, product_id);The best indexes depend on the database and its execution plan. Check that plan with your database’s inspection command. The optimizer may transform nested NOT EXISTS conditions into anti-joins, so the written nesting does not necessarily mean repeated full-table scans.
Exercise
How would you change the query so that a customer qualifies only when they have ordered every product and the products table is not empty?
Show answer
Add EXISTS (SELECT 1 FROM products) to the customer filter, alongside the outer NOT EXISTS condition.