intermediate
Anti-semi Join
5 min readLast updated: 2026-07-23
Overview
An Anti-semi Join (or Anti Join) returns rows from the left table that have no match in the right table.
Learning Objectives
- Understand Anti-semi Join execution logic.
- Implement Anti Joins using
NOT EXISTSorLEFT JOIN ... WHERE right.id IS NULL. - Filter out records present in secondary datasets efficiently.
Detailed Concept Explanation
Anti-semi Joins complement Semi Joins. Instead of keeping rows that match, an Anti-semi Join keeps rows from the left table that have zero matching records in the right table.
In SQL, Anti Joins are typically written using:
NOT EXISTSLEFT JOIN ... WHERE right_table.key IS NULL
Code Examples
SQL
sql
-- Anti Join using NOT EXISTS (Recommended)
SELECT c.customer_id, c.name
FROM customers c
WHERE NOT EXISTS (
SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
);
-- Anti Join using LEFT JOIN
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o ON c.customer_id = o.customer_id
WHERE o.customer_id IS NULL;
Best Practices
- Use NOT EXISTS:
NOT EXISTSis generally cleaner and less error-prone for Anti Joins thanNOT IN(which breaks whenNULLvalues are present).
Interview Perspective
Note
Interview Question: What is an Anti Join and when would you use it?
An Anti Join identifies records in one table that do not exist in another (e.g. finding inactive users, missing logs, or unfulfilled orders).
Interactive Challenges
Summary
Anti-semi Joins return records from a primary table that have no matching entries in a secondary table.