NEW
Also known as: Left Anti Join
A relational operation that returns rows from one table with no matching rows in another.
DESCRIPTION
An anti join filters a dataset to only those rows that lack a match in a second dataset. In SQL, it is commonly implemented with NOT EXISTS, NOT IN, or LEFT JOIN with a null check. Distributed engines and some databases provide a native LEFT ANTI JOIN for clarity and performance. It is widely used for de-duplication, change-data capture comparisons, and identifying orphan records. Proper handling of NULL semantics is critical to avoid incorrect results.
ALTERNATIVE DEFINITIONS
Wikipedia
In relational algebra, the antijoin is a binary operation that returns the tuples in one relation that do not match tuples in another relation.
Frames antijoin in formal relational algebra as a binary set operation.
Snowflake
LEFT ANTI JOIN returns only the rows from the left table that do not have a matching row in the right table.
Gives a concrete SQL join type with precise behavioral wording.
Databricks
A left anti join returns only rows from the left side that do not have a match on the right side.
Explains anti join behavior for distributed processing on Databricks and Spark.
RELATED TERMS
Join Inner Join Left Join NOT EXISTS EXCEPT Relational Algebra SQL Set Difference
COMPETING TERMS
RELATIONS
- Broader: Join
KNOWLEDGE AREAS
Programming Languages Data Engineering Data Modeling Data Warehousing & BI
TARGET AUDIENCE
Data Engineer Analytics Engineer Data Scientist Data Analyst Software Developer Data Modeler
EXTERNAL SOURCES
- Wikipedia - Antijoin
- Snowflake - Official join types reference including anti join
- Apache Spark - Spark SQL join types and semantics
- Oracle - Oracle optimizer anti-join concepts
TOP QUESTIONS
What is an Anti Join used for?
Anti joins detect records in one dataset that do not exist in another. They are commonly used for de-duplication, orphan detection, and validating referential completeness.
Category: UseCases
How do you write an Anti Join in SQL?
Typical patterns are NOT EXISTS, NOT IN with proper NULL handling, or LEFT JOIN plus WHERE right_key IS NULL. Many engines also support a LEFT ANTI JOIN syntax.
Category: HowTo
What is the difference between Anti Join and Semi Join?
An anti join returns only non-matching rows from the left input, while a semi join returns only matching rows from the left input without duplicating right-side columns.
Category: Comparison
What are common mistakes when using Anti Join?
Using NOT IN against a column containing NULLs can filter out all rows unexpectedly. Prefer NOT EXISTS or careful NULL-safe comparisons to avoid logic errors.
Category: Mistakes
When is Anti Join preferred over EXCEPT?
Use anti join when comparing based on key relationships or when additional predicates are needed. EXCEPT is simpler for set difference on identical schemas.
Category: Guidance
How do query optimizers implement Anti Join?
Optimizers rewrite NOT EXISTS or equivalent forms into anti-join plans, often using hash anti-join strategies for scalable performance.
Category: Architecture
Why does NULL handling matter for Anti Join?
Three-valued logic means NOT IN with NULLs can yield UNKNOWN, excluding rows unexpectedly. Choosing NOT EXISTS avoids this pitfall.
Category: Risk
Is Anti Join supported in distributed engines?
Yes. Systems like Spark, Databricks, and Snowflake support left anti join semantics natively for scalable non-match filtering.
Category: Capabilities
What performance tips help Anti Join at scale?
Index join keys, reduce data early with predicates, and ensure compatible data types. On distributed systems, broadcast small tables when possible.
Category: Performance