When you need it
"Customers who never ordered", "products with no sales", "accounts with no invoice this year", "tickets nobody answered". Words that signal it: "never", "no", "without", "not in", "haven't", "missing", "didn't".
You are looking for rows in table A that have no partner in table B. The answer is a list of A rows, not a combination of A and B.
The shape of the query
Three forms give the same answer when written correctly.
-- 1. NOT EXISTS
select a.* from a
where not exists (select 1 from b where b.a_id = a.id);
-- 2. LEFT JOIN with an IS NULL test
select a.* from a
left join b on b.a_id = a.id
where b.a_id is null;
-- 3. NOT IN (risky)
select a.* from a
where a.id not in (select a_id from b);Step by step:
- Decide which table lists the things you want to find (the A side).
- Decide what counts as a match. "Never ordered" might mean no order at all, or no completed order. Put that condition inside the subquery or the
ONclause. - Keep A rows where no match exists.
select c.customer_id, c.full_name
from customers c
where not exists (
select 1
from orders o
where o.customer_id = c.customer_id
and o.status = 'completed'
)
order by c.customer_id
limit 10;The same question with a left join:
select p.product_id, p.product_name
from products p
left join order_items oi on oi.product_id = p.product_id
where oi.product_id is null
order by p.product_id;Test for NULL on a column that cannot be NULL in a real match, such as the join key from the right table. Testing a column that can be NULL itself gives false positives.
Traps interviewers look for
NOT INand NULLs. If the subquery returns even one NULL,NOT INreturns no rows at all, because each comparison becomes unknown. UseNOT EXISTS, or addwhere col is not nullinside the subquery.- Filter in the wrong place. With a left join, a condition on the right table such as
o.status = 'completed'goes inON. InWHEREit deletes the NULL rows you are looking for and returns nothing. - Testing the wrong column.
where o.status is nullalso matches orders whose status is NULL. Test a key that must exist. - Join multiplication. A left join to a many-side table repeats A rows before the filter. The filter then removes all repeats, so it still works, but if you forget the
IS NULLtest you will see duplicates. - Defining "no match" too loosely. "Customers who did not order in 2025" means no order within 2025, not no order ever. Put the date range inside the match condition.
- Counting the missing. "How many customers never ordered" is
count(*)over the anti-join result, not a difference of two counts when duplicates exist.
Variations
- Missing in a period. Users active in January but not in February: match on January, anti-join February.
- Set difference.
exceptreturns rows in the first query that are not in the second, with duplicates removed and NULLs treated as equal. - Reverse direction. Rows in B that have no A (orphans) are the same pattern with the tables swapped. Useful for data quality checks.
- Gaps in a sequence. An id range joined to the table shows the missing ids.
- Both sides. A full outer join with a check on each side finds rows present in only one of two tables.
In other warehouses
NOT EXISTSand theLEFT JOIN ... IS NULLform work in Snowflake, BigQuery, SQL Server, MySQL, Redshift and Databricks.- Databricks also offers
LEFT ANTI JOIN, which says exactly what you mean. - BigQuery and Snowflake treat
NOT INwith NULLs the same way as PostgreSQL. The trap is not specific to one engine. - Set difference is
EXCEPTin PostgreSQL, SQL Server, BigQuery (asEXCEPT DISTINCT), Snowflake and Databricks. Snowflake also acceptsMINUS.