Semi-Joins & Anti-Joins (EXISTS, NOT EXISTS, IN, NOT IN Pitfalls with NULL)
Express existence checks safely and avoid NOT IN + NULL traps.
In plain words
EXISTS asks "is there at least one match?" and NOT EXISTS asks "is there no match at all?". You get the original rows back, once each.
Think of it like…
Semi-join: "list the students who handed in at least one assignment"; you don't care how many. Anti-join: "list the students who handed in nothing".
Where you’ve seen it
A win-back campaign targets customers with no order in 90 days: SELECT c.* FROM customers c WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id AND o.created_at > now() - interval '90 days').
Words you’ll hear
- Semi-join
- Rows with at least one match: EXISTS or IN.
- Anti-join
- Rows with no match: NOT EXISTS or LEFT JOIN … IS NULL.
- Correlated subquery
- A subquery that refers to the outer row, as in o.customer_id = c.id.
- NOT IN NULL trap
- x NOT IN (…, NULL) is never TRUE, so the query returns no rows.
Visual diagram
Theory & example queries
Semi-joins: EXISTS vs IN vs JOIN
EXISTS can stop as soon as it finds one match, and each outer row appears at most once. IN (subquery) has the same meaning, and modern optimizers usually produce the same plan for both.
An INNER JOIN is not a semi-join: a customer with 10 orders appears 10 times. Adding DISTINCT hides the duplicates, but the database still builds the larger result and then deduplicates it.
| id | name | city | |
|---|---|---|---|
| 1 | Maya | maya@example.com | Pune |
| 2 | Ravi | ravi@example.com | Delhi |
| 3 | Noor | noor@example.com | Mumbai |
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT c.*FROM customers cWHERE EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.id );PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Customers with at least one order2 sample rows
| id | name | city |
|---|---|---|
| 1 | Asha | Pune |
| 3 | Kabir | Delhi |
Note: Assumes these are the only sample customers with matching orders; columns reflect an illustrative customers table.
Key takeaway: For "has at least one", use EXISTS, not JOIN + DISTINCT.
Anti-joins and the NOT IN trap
WHERE id NOT IN (SELECT customer_id FROM orders) means id <> a AND id <> b AND id <> NULL …. The comparison with NULL is UNKNOWN, so the whole condition is never TRUE and the query returns zero rows.
NOT EXISTS doesn't have this problem because it only asks whether a matching row exists. LEFT JOIN … WHERE o.id IS NULL also works, as long as you test a column that is never NULL on real matches, such as the primary key.
| id | name | city | |
|---|---|---|---|
| 1 | Maya | maya@example.com | Pune |
| 2 | Ravi | ravi@example.com | Delhi |
| 3 | Noor | noor@example.com | Mumbai |
| id | customer_id | status | amount | coupon_code |
|---|---|---|---|---|
| 101 | 1 | paid | 249 | NULL |
| 102 | 2 | paid | 120 | WELCOME10 |
| 103 | 1 | shipped | 89.5 | NULL |
SELECT c.*FROM customers cWHERE NOT EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.id );PostgreSQL is the lesson baseline unless noted otherwise.
Calculate the selected expressions and return the requested output columns.
Customers with no orders2 sample rows
| id | name | city |
|---|---|---|
| 2 | Leena | Jaipur |
| 4 | Mina | Pune |
Note: Assumes these sample customers have no matching row in orders.
Key takeaway: Default to NOT EXISTS for "has no match".
How it works, step by step
Follow one practical SQL workflow from start to finish. Select any step or play through the flow.
Has a match, or has none?
"Customers who ordered" is a semi-join; "customers who never ordered" is an anti-join. Neither needs columns from orders.
EXISTS vs NOT EXISTS
When to use it & trade-offs
What it is
A semi-join returns rows from one table that have *at least one* match in another (EXISTS, IN). An anti-join returns rows that have *no* match (NOT EXISTS, LEFT JOIN … IS NULL, NOT IN). Neither adds columns from the other table or duplicates rows.
Why it matters: "Users who placed an order" and "users who never did" are among the most common interview and analytics questions. The trap is NOT IN: a single NULL in the subquery makes it return nothing.
When to reach for it
- Finding users who never converted, churned, or have no profile
- Filtering to entities that have at least one related event
- Data quality checks, such as orders whose customer no longer exists
When to avoid or be careful
- NOT IN against a nullable column
- INNER JOIN + DISTINCT when you only need to know a match exists
- Putting the time-window filter outside the subquery, which changes the meaning
Ways to write "customers with no orders"
Strengths: NULL-safe, clear intent, planned as an anti-join.
Watch out: Slightly more typing.
Strengths: Works everywhere and usually gets the same plan.
Watch out: You must test a non-nullable column, and extra WHERE conditions easily break it.
Strengths: Reads naturally.
Watch out: Returns no rows at all if the subquery contains a NULL.
Interview signals & numbers to know
When this topic comes up
- “Customers who never placed an order”
- “Users with at least one purchase”
- NOT IN returns nothing
- EXISTS vs IN
- A JOIN produces duplicate rows
Benchmarks & rules of thumb
- 1
- match is enough for EXISTS to stop
- 1 NULL
- is enough to break NOT IN
- 0
- extra rows added by a semi-join
Common mistakes & follow-ups
Common interview mistakes
- Using NOT IN when the subquery column contains NULLs.
- Using a JOIN for an existence check and then patching the duplicates with DISTINCT.
- In the LEFT JOIN version, testing IS NULL on a column that can legitimately be NULL.
Likely interviewer follow-ups
- Why does NOT IN behave differently from NOT EXISTS when NULLs are present?
- Is IN faster than EXISTS? What does the optimizer actually do?
- How would you find products that weren't ordered in the last 30 days?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Semi-Joins & Anti-Joins (EXISTS, NOT EXISTS, IN, NOT IN Pitfalls with NULL) with hands-on SQL practice and interview Q&A.