Skip to content
Bronze 7 min read · Joins 2 practice problems ↓

Semi and anti joins

Some questions are only about whether a match exists: which customers have ever ordered, which have never ordered. A normal join answers them clumsily, repeating a customer once per order. Semi and anti joins answer them directly, and the anti join avoids a famous NOT IN trap with NULL.

Foundations · SQL for data engineers · Bronze · Joining tables, lesson 2 of 3

Read first: Inner and outer joins · NULL and three-valued logic (skip if you know them)

In plain words first

A teacher has a class list and a pile of handed-in homework. "Who handed something in?" needs a tick next to each name, once, however many sheets that student handed in. "Who handed nothing in?" needs the names with no sheet at all.

The first question is a semi join, the second an anti join. Neither copies anything from the homework pile: they only look at whether a match exists.

Questions about existence

"Which customers have ordered?" and "which customers have never ordered?" do not need any columns from orders; they only ask whether a match exists. A join answers the wrong question: it returns a row per match, so Asha, with three orders, would appear three times. Semi and anti joins return each left row at most once, and only columns from the left table.

Semi join

Left rows that have at least one match. Like an inner join that never duplicates.

Anti join

Left rows that have no match. The rows an inner join would drop.

EXISTS: has at least one match

Spark SQL · Customers who have ordered

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenullNULL: A missing or unknown value. It is not 0 or an empty string, and any comparison with it is unknown instead of true or false. Learn more →delivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
SELECT c.customer_id, c.name
FROM customers c
WHERE EXISTS (
  SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
)
ORDER BY c.customer_id

The subquery refers to c, the outer row: it is correlated, so it is checked for each customer. What it selects does not matter, so SELECT 1 is the convention. Asha appears once. The engine can stop at the first match, and in Spark the query plan shows a LeftSemi join.

Spark also lets you write the semi join directly, which is handy in long queries:

Spark SQL · Spark's own syntax

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
SELECT c.customer_id, c.name
FROM customers c
LEFT SEMI JOIN orders o ON o.customer_id = c.customer_id
ORDER BY c.customer_id

WHERE customer_id IN (SELECT customer_id FROM orders) is a third way to write the same semi join, and it is safe: NULLs in the subquery cannot make IN go wrong in the way they break NOT IN.

NOT EXISTS: has no match

Spark SQL · Customers who have never ordered

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
  SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
)

Only Zoya. The anti join answers questions like "products never sold", "users who did not log in this week" or "rows in the extract missing from the warehouse". Spark's own form is LEFT ANTI JOIN:

Spark SQL · LEFT ANTI JOIN, with an extra condition

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
SELECT c.customer_id, c.name
FROM customers c
LEFT ANTI JOIN orders o
  ON c.customer_id = o.customer_id AND o.status = 'delivered'

With the extra condition in ON, the question becomes "customers with no delivered order": Meera (her only order has no status) and Zoya.

The NOT IN trap, again

NOT IN looks like the natural anti join, and it works until the subquery column contains a NULL. Then id NOT IN (..., NULL) is never true, and the query returns nothing, with no error:

Spark SQL · Who referred nobody? No rows, because referred_by has NULLs
SELECT name
FROM customers
WHERE customer_id NOT IN (SELECT referred_by FROM customers)

Output

(no rows)
Spark SQL · The same question with NOT EXISTS

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
SELECT c.name
FROM customers c
WHERE NOT EXISTS (
  SELECT 1 FROM customers r WHERE r.referred_by = c.customer_id
)
ORDER BY c.name

Rule of thumb: write anti joins with NOT EXISTS (or LEFT ANTI JOIN in Spark), never with NOT IN over a column that can be NULL. Adding WHERE referred_by IS NOT NULL inside the subquery also fixes it, but NOT EXISTS needs no such care.

The LEFT JOIN ... IS NULL pattern

Before EXISTS was common, anti joins were written as a LEFT JOIN that keeps only the rows without a match. You will meet it in old code and in engines with weak subquery support:

Spark SQL · Anti join as LEFT JOIN + IS NULL

Input: orders

order_idcustomer_idrestaurantcityamountstatusplaced_at
1011Spice RoutePune420delivered2026-03-01 12:10:00
1022Pizza LabDelhi650cancelled2026-03-01 19:40:00
1031Pizza LabPunenulldelivered2026-03-02 13:05:00
1043Spice RoutePune310null2026-03-03 20:15:00
1052Wok InnDelhi280delivered2026-03-04 21:00:00
1064Spice RoutePune540delivered2026-03-05 13:30:00
1079Wok InnMumbai190delivered2026-03-05 20:45:00
1081Wok InnPune360refunded2026-03-06 19:10:00

Input: customers

customer_idnamecitysignup_datereferred_by
1AshaPune2026-01-05null
2RaviDelhi2026-01-201
3Meeranull2026-02-021
4KabirPune2026-02-142
5ZoyaMumbai2026-03-01null
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL

Test a column that can only be NULL when there was no match, such as the right table's key. Testing a column that can be NULL on its own, like o.status, would also return Meera, whose order has no status.

Common mistakes

  • Using a join to test for existence — An inner join repeats the left row once per match. Use EXISTS or LEFT SEMI JOIN, or you will need a DISTINCT to undo the damage.
  • NOT IN over a nullable column — One NULL in the subquery and the query returns no rows. Use NOT EXISTS or LEFT ANTI JOIN.
  • Testing the wrong column after LEFT JOIN — In the IS NULL pattern, test the right table's key, not a column that can be NULL in matched rows too.
  • Selecting right-side columns from a semi join — A semi or anti join returns only the left table's columns. If you need columns from both sides, you need a real join.

Key takeaways

  • Semi joins keep left rows with a match, anti joins keep left rows without one, each at most once.
  • EXISTS / NOT EXISTS work everywhere; Spark adds LEFT SEMI JOIN and LEFT ANTI JOIN.
  • NOT IN returns nothing once the subquery has a NULL. Prefer NOT EXISTS.
  • In the LEFT JOIN ... IS NULL pattern, test the right table's key.

Interview prep

THE QUESTION

"Find the customers who have never placed an order. Give two ways, and tell me which one you would not use."

NOT EXISTS: WHERE NOT EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id), or in Spark LEFT ANTI JOIN orders o ON ...; the LEFT JOIN ... WHERE o.order_id IS NULL pattern also works. I would avoid NOT IN (SELECT customer_id FROM orders): if any customer_id in orders is NULL, the condition is never true and the query silently returns nothing. All the safe forms compile to the same anti join in Spark.

Avoid: "WHERE id NOT IN (SELECT customer_id FROM orders)." It is correct only while orders.customer_id has no NULLs, and the interviewer will ask what happens when it does.

Follow-ups · answer out loud, then open

Follow-up"What is the difference between an inner join and a semi join?"
An inner join returns one row per matching pair and can return columns from both tables. A semi join returns each left row at most once, with only left columns, and only tests whether a match exists. Inner join plus DISTINCT imitates a semi join but does more work and breaks if the left table has real duplicates.
Scenario"Find the products that were viewed this week but never added to a cart."
An anti join with the extra condition inside: SELECT DISTINCT v.product_id FROM views v WHERE v.day >= current_date() - 7 AND NOT EXISTS (SELECT 1 FROM cart_adds a WHERE a.product_id = v.product_id). Decide whether a cart add from any time counts or only this week, and put that date condition inside the subquery.
Trap"IN (subquery) has the same NULL problem as NOT IN."
No. x IN (1, NULL) is true when x is 1, and NULL otherwise, so the matching rows still pass; the NULL only stops non-matching rows, which WHERE would drop anyway. It is the negation, NOT IN, that turns every row into NULL.

Check yourself

4 questions

Asha has 3 orders. How many times does she appear in ... WHERE EXISTS (orders for c)?

Show the answer

1. A semi join returns each left row at most once.

Which columns can a LEFT SEMI JOIN return?

Show the answer

Left table only. Semi and anti joins only test for a match; they return the left rows.

The subquery of NOT IN returns 1, 2 and NULL. Which ids pass?

Show the answer

None. id <> NULL is never true, so the whole condition is never true.

In LEFT JOIN orders o ... WHERE ??? IS NULL, which column should you test?

Show the answer

o.order_id. Test the right table's key: it is NULL only when there was no match.

Practise this

Problems that use this, graded on hidden tests

Keep going

Up next · Joining tables · lesson 3 of 3 · 5 min read
Self joins
Join a table to itself to compare its rows: who referred whom, pairs of orders, and duplicates that differ in one column.

Related lessons

Previous: Inner and outer joins

Primary sources: JOIN (semi and anti joins) · EXISTS and IN subqueries · NULL semantics (IN and NOT IN)