Stop 06 · Joining TablesTopics · 9/10

Anti Joins (Finding Unmatched Rows)

Part of the SQL Roadmap

In short

Some of the most valuable questions are about absence: customers who never ordered, products that never sold, students who never logged in. An anti join finds the rows that have no partner.

An anti join finds rows in one table with no match in another, usually written as LEFT JOIN ... WHERE right.id IS NULL or with NOT EXISTS.

The jaadu stands in a gym lobby holding a long member list, crossing names off while a pile of unused membership cards gathers dust on a shelf behind it.

Think of it like this

The gym's sleeping members

A gym in Bengaluru has a list of all paid members and a log of every card swipe at the door. To find members who paid but never came, the manager goes down the member list and crosses off anyone who appears in the swipe log. Whoever is left uncrossed is the answer. An anti join keeps exactly those leftover rows.

Watch it happen

Customers with no orders

Asha
0
Ravi
1
Meena
2
Farhan
3
Divya
4

1/4Five customers. We LEFT JOIN orders onto each one.

The big ideas

What you need to know

LEFT JOIN + IS NULL

Join, then keep rows where the right side's key is NULL. Classic and widely understood.

NOT EXISTS

WHERE NOT EXISTS (SELECT 1 FROM orders WHERE ...) reads like the question and is just as fast in modern databases.

Avoid NOT IN

If the subquery returns even one NULL, NOT IN returns no rows at all. NOT EXISTS doesn't have this trap.

Check the key

Test IS NULL on a column that's never NULL in real rows, like the primary key, not an optional column.

See it in code

Walk through the code

sql
1-- Version 1: LEFT JOIN, then keep the unmatched rows2SELECT c.customer_id, c.name, c.signup_date3FROM customers AS c4LEFT JOIN orders AS o5  ON o.customer_id = c.customer_id6WHERE o.order_id IS NULL;7 8-- Version 2: NOT EXISTS, same result9SELECT c.customer_id, c.name, c.signup_date10FROM customers AS c11WHERE NOT EXISTS (12  SELECT 113  FROM orders AS o14  WHERE o.customer_id = c.customer_id15);

1/3LEFT JOIN keeps everyone, then WHERE keeps only rows where no order matched.

Customers who signed up but never ordered, written two ways. Both work in PostgreSQL, MySQL and SQL Server.

Save yourself the pain

Common mistakes

Oops

WHERE customer_id NOT IN (SELECT customer_id FROM orders)

Do this instead

One NULL customer_id in orders makes it return nothing. Use NOT EXISTS or LEFT JOIN ... IS NULL.

Oops

WHERE o.coupon_code IS NULL to find unmatched rows.

Do this instead

Matched orders without a coupon also have NULL there. Test the right table's primary key.

Oops

Putting the IS NULL check in ON.

Do this instead

In ON it just prevents matches, so every customer comes back. The IS NULL check belongs in WHERE.

Your mission

Find what's missing

~20 min

Quick check

Which anti join pattern is broken if the subquery can return a NULL?

Go deeper

  1. 1Find customers who have never placed an order.
  2. 2Write the same query with NOT EXISTS and compare.
  3. 3Learn why NOT IN can return nothing if the subquery contains a NULL.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge