Sub topic 5
NOT EXISTS: finding rows with no match
Assume you know SELECT, WHERE, table aliases, INNER JOIN, LEFT JOIN, aggregate functions, and NULL. This chapter focuses on the working pattern for finding rows that have no related rows.
NOT EXISTS is a SQL condition that is true when a subquery returns no rows. It is useful for questions such as:
- Which customers have never placed an order?
- Which products have no reviews?
- Which employees are not assigned to a project?
The correlated pattern
A correlated subquery is an inner query that refers to a column from the outer query. The outer query supplies one current row, and the inner query checks that row's related data.
Suppose customers contains one row per customer and orders contains orders linked by customer_id:
SELECT c.customer_id, c.name
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
);For each customer, the database evaluates the inner query with that customer's ID:
- Look for an order whose
customer_idmatches the current customer. - If at least one order is found,
EXISTSis true. NOT EXISTSreverses that result.- The customer is returned only when no matching order exists.
The condition o.customer_id = c.customer_id is the correlation. The c alias belongs to the outer query, while o belongs to the inner query.
Why `SELECT 1` is conventional
The selected value inside EXISTS does not affect the result. SQL only needs to know whether the subquery returns at least one row. Therefore, these forms have the same meaning:
WHERE NOT EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
)WHERE NOT EXISTS (
SELECT o.order_id
FROM orders AS o
WHERE o.customer_id = c.customer_id
)SELECT 1 communicates the intent clearly: the query tests for a row, not a value.
> callout note > Keep the correlation condition inside the subquery. If you omit o.customer_id = c.customer_id, the subquery checks whether any order exists anywhere in the table. Then either every customer or no customer may be returned, depending on whether the table is empty.
`NOT EXISTS` compared with a `LEFT JOIN`
The same absence check can often be written as an anti-join. An anti-join returns rows from one table that have no matching row in another table.
SELECT c.customer_id, c.name
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;The LEFT JOIN keeps every customer. A customer with no order receives NULL values for the order columns, so WHERE o.order_id IS NULL selects those customers.
NOT EXISTS usually expresses the question more directly. A LEFT JOIN can be useful when you also need columns from the related table, but be careful when selecting a nullable column. Test a column that cannot be null for a real order, such as a primary key.
| Form | Best fit | Common concern |
|---|---|---|
| `NOT EXISTS` | Test whether no related row exists | The correlation must be correct |
| `LEFT JOIN ... IS NULL` | Return unmatched rows and possibly join data | Nullable checks and join duplicates |
Why `NOT EXISTS` handles `NULL` safely
NOT EXISTS checks whether a matching row was found. It does not compare the outer value against every value in a list. This makes it safer than NOT IN when the inner column might contain NULL.
For example, this query can fail to return any rows if orders.customer_id contains a NULL:
SELECT c.customer_id, c.name
FROM customers AS c
WHERE c.customer_id NOT IN (
SELECT o.customer_id
FROM orders AS o
);Use the correlated form when you mean “there is no order for this customer”:
SELECT c.customer_id, c.name
FROM customers AS c
WHERE NOT EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.customer_id
);The database can also stop searching for a customer as soon as it finds the first matching order. An index helps it perform that lookup efficiently:
CREATE INDEX idx_orders_customer_id
ON orders (customer_id);Exercise
Write a query that returns products from products that have no matching row in order_items, where both tables use product_id.
Show answer
SELECT p.productid, p.name FROM products AS p WHERE NOT EXISTS ( SELECT 1 FROM orderitems AS oi WHERE oi.productid = p.productid );