left_semi / left_anti
"Customers who have ordered" and "customers who have never ordered" only ask whether a match exists. A normal join answers them clumsily and repeats rows. Semi and anti joins answer them directly. This lesson shows both, and why the anti join is safe where NOT IN with nulls is not.
Step 1 · PySpark functions · Silver · Reshape and refine, lesson 2 of 7
On this page
Show code in
Every code block on the page follows this.
Read first: join (skip if you know it)
In plain words first
A teacher with a class list and a pile of homework can ask two questions: "who handed something in?" (tick each name once, however many sheets) and "who handed nothing in?". Neither needs to copy anything from the homework itself.
The first is a semi join, the second an anti join: filters on the left table, driven by whether a match exists on the right.
What they do
Semi and anti joins answer existence questions: "customers who ordered" and "customers who never ordered". They are filters on the left table, driven by the right table.
Step by step
customers, orders for customers 1, 1, 3
| customer_id | name |
|---|---|
| 1 | Asha |
| 2 | Ben |
| 3 | Chitra |
| 4 | Dev |
left_semi
| customer_id | name |
|---|---|
| 1 | Asha |
| 3 | Chitra |
Asha has two orders but appears once. An inner joininner join: A join that keeps only the rows that have a match on both sides. Learn more → would have returned her twice. left_anti returns Ben and Dev.
Run the example
Input: customers
| customer_id | name |
|---|---|
| 1 | Asha |
| 2 | Ben |
| 3 | Chitra |
| 4 | Dev |
Input: orders
| order_id | customer_id |
|---|---|
| 10 | 1 |
| 11 | 1 |
| 12 | 3 |
| 13 | nullnull: A missing or unknown value. Any comparison with null gives null, so filters treat it as false. Learn more → |
result = customers.join(orders, on="customer_id", how="left_anti")
SELECT * FROM customers c WHERE NOT EXISTS ( SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id)
Spark SQL also accepts LEFT ANTI JOIN and LEFT SEMI JOIN directly; EXISTS / NOT EXISTS subqueries are planned as the same joins.
Why not an inner join and distinct?
An inner join emits a row per match, so customers with many orders are repeated and you need a distinct afterwards, which is an extra shuffleshuffle: Moving rows between machines so that all rows with the same key end up together. Needed by joins, groupBy and sorting, and usually the most expensive step of a job. Learn more → on wide rows. A semi join stops at the first match and never duplicates.
The NOT IN null trap
In SQL, WHERE customer_id NOT IN (SELECT customer_id FROM orders) returns no rows at all if the subquery contains a null, as our orders table does. x NOT IN (1, 3, null) means x <> 1 AND x <> 3 AND x <> null, and the last part is null for every x.
| Query | Result with a null in orders |
|---|---|
NOT IN (subquery) | Empty: nothing passes |
NOT EXISTS / left_anti | Ben and Dev, as expected |
Spark plans NOT IN as a special null-aware anti join to preserve those semantics. Prefer NOT EXISTS or left_anti, which mean what people intend.
Under the hood Optional deep dive
They use the same strategies as other joins: if the right side is small, it is broadcastbroadcast join: A join where the small table is copied to every machine, so the big table never has to be shuffled. Learn more → and the left side is not shuffled. Only the join keys of the right side matter, so select just the key columns from the right before joining to keep the broadcast small.
Common mistakes
- Inner join plus distinct for existence — Duplicates first, then an extra shuffle to remove them. Use left_semi.
- NOT IN with nullable keys — Returns nothing. Use NOT EXISTS or left_anti.
- Expecting right-side columns — Semi and anti joins return only left columns.
Key takeaways
- left_semi keeps left rows with a match; left_anti keeps rows without one.
- Only left columns come back, and no row is duplicated.
- NOT IN returns nothing if the subquery has a null; NOT EXISTS does not.
- Small right sides are broadcast, so select only their keys.
Interview prep
THE QUESTION
"Find customers with no orders. Why might NOT IN return zero rows?"
left_anti or NOT EXISTS. NOT IN returns nothing if the subquery yields any null, because comparisons with null are unknown. Showing that you know the trap, and that Spark handles it with a null-aware anti join, is the strong answer.Avoid: "a left join and filter where right.id is null" without the alternatives. It works, but left_anti says what you mean and never duplicates rows.
Follow-ups · answer out loud, then open
Follow-up"How is a left_semi join different from an inner join followed by selecting the left columns?"
Scenario"Find products that were ordered in 2024 but not in 2025."
Trap"NOT IN and NOT EXISTS always return the same rows."
Check yourself
3 questionsA customer has 3 orders. How many times does it appear in a left_semi join with orders?
Show the answer
1. A semi join keeps each left row at most once.
The subquery returns 1, 3 and null. What does WHERE id NOT IN (subquery) return?
Show the answer
No rows. id <> null is unknown for every id, so the whole NOT IN is never true.
Which columns does left_anti return?
Show the answer
Left only. Semi and anti joins act as filters on the left table.
Practise this
Problems that use left_semi / left_anti, graded on hidden tests
Keep going
Up next · Reshape and refine · lesson 3 of 7 · 4 min readeqNullSafe (<=>)
Equality where null matches null: the fix for joins and comparisons that silently drop null keys.
Related lessons
Previous: coalesce
Primary sources: DataFrame.join · JOIN types (SQL)