PySpark SQL Left Anti Join with Example

Spread the love

When you join two DataFrames using Left Anti Join (leftanti), it returns only columns from the left DataFrame for non-matched records.

In this PySpark article, I will explain how to do Left Anti Join(leftanti/left_anti) on two DataFrames with PySpark & SQL query Examples.

leftanti join does the exact opposite of the leftsemi join.

Before we jump into PySpark Left Anti Join examples, first, let’s create an emp and dept DataFrames. here, column emp_id is unique on emp and dept_id is unique on the dept DataFrame and emp_dept_id from emp has a reference to dept_id on dept dataset.

import pyspark
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName("").getOrCreate()

emp = [(1,"Smith",-1,"2018","10","M",3000), \
    (2,"Rose",1,"2010","20","M",4000), \
    (3,"Williams",1,"2010","10","M",1000), \
    (4,"Jones",2,"2005","10","F",2000), \
    (5,"Brown",2,"2010","40","",-1), \
      (6,"Brown",2,"2010","50","",-1) \
empColumns = ["emp_id","name","superior_emp_id","year_joined", \

empDF = spark.createDataFrame(data=emp, schema = empColumns)

dept = [("Finance",10), \
    ("Marketing",20), \
    ("Sales",30), \
    ("IT",40) \
deptColumns = ["dept_name","dept_id"]
deptDF = spark.createDataFrame(data=dept, schema = deptColumns)

This prints emp and dept DataFrame to the console.

# Emp Dataset
|emp_id|name    |superior_emp_id|year_joined|emp_dept_id|gender|salary|
|1     |Smith   |-1             |2018       |10         |M     |3000  |
|2     |Rose    |1              |2010       |20         |M     |4000  |
|3     |Williams|1              |2010       |10         |M     |1000  |
|4     |Jones   |2              |2005       |10         |F     |2000  |
|5     |Brown   |2              |2010       |40         |      |-1    |
|6     |Brown   |2              |2010       |50         |      |-1    |

# Dept Dataset
|Finance  |10     |
|Marketing|20     |
|Sales    |30     |
|IT       |40     |

PySpark Left Anti Join (leftanti) Example

In order to use left anti join, you can use either antileftantileft_anti as a join type.

empDF.join(deptDF,empDF.emp_dept_id == deptDF.dept_id,”leftanti”) \ .show(truncate=False)

Yields below output

|emp_id|name |superior_emp_id|year_joined|emp_dept_id|gender|salary|
|6     |Brown|2              |2010       |50         |      |-1    |

Using PySpark SQL Left Anti Join

Let’s see how to use Left Anti Join on PySpark SQL expression, In order to do so first let’s create a temporary view for EMP and DEPT tables.


joinDF2 = spark.sql("SELECT e.* FROM EMP e LEFT ANTI JOIN DEPT d ON e.emp_dept_id == d.dept_id") \

This also returns the same output as above.


In this PySpark article, you have learned left anti join which is used to get only columns from the left DataFrame for non-matched records.

Hope you Like it !!


Naveen (NNK)

I am Naveen (NNK) working as a Principal Engineer. I am a seasoned Apache Spark Engineer with a passion for harnessing the power of big data and distributed computing to drive innovation and deliver data-driven insights. I love to design, optimize, and managing Apache Spark-based solutions that transform raw data into actionable intelligence. I am also passion about sharing my knowledge in Apache Spark, Hive, PySpark, R etc.

Leave a Reply

You are currently viewing PySpark SQL Left Anti Join with Example