Featured Article

Master PySpark Join Types with Real World Examples A Complete Guide

Kenneth Jul 13, 2026

Spark SQL, a component of Apache Spark, offers various types of joins to combine datasets based on specific key columns. Understanding these join types is crucial for efficiently manipulating data in PySpark. Let's delve into the different join operations available in PySpark, complete with examples to illustrate their functionality.

Pinball Game development using Python
Pinball Game development using Python

First, let's import the necessary libraries and create two DataFrames for demonstration. We'll use the student IDs as the common key for joining operations.

Timber Frame Joinery Archives
Timber Frame Joinery Archives

PySpark Join Types

PySpark provides five types of joins: inner, outer, full outer, left, and right. Here's an overview of each join type.

a diagram of the different types of jointions and their functions in human body anatomy
a diagram of the different types of jointions and their functions in human body anatomy

In the following examples, assume that we have the following DataFrames:

  • df1: student ID, name, age
  • df2: student ID, department
Joints
Joints

Inner Join

The inner join returns only the rows where there is a match in both DataFrames based on the key column. This is the default join type in PySpark.

Python code for inner join:

Wood Joinery Guide
Wood Joinery Guide

df_result = df1.join(df2, df1['student_id'] == df2['student_id'], 'inner')

Outer Join

Outer joins are further classified into left, right, and full outer joins. They return all the rows from one DataFrame and the matching rows from the other DataFrame. If there's no match, the result will contain null values.

different types of wood joinrs on a white background with black border around the edges
different types of wood joinrs on a white background with black border around the edges

Left Outer Join

The left join returns all the rows from the left DataFrame (df1) and the matching rows from the right DataFrame (df2). If no match is found, it includes null values.

a man in a business suit holding a clipboard next to a laptop and money
a man in a business suit holding a clipboard next to a laptop and money
the different types of bearing types are shown in this diagram, and there is also an explanation
the different types of bearing types are shown in this diagram, and there is also an explanation
common Electrical Splices
common Electrical Splices
an instruction sheet with instructions on how to use the faucet
an instruction sheet with instructions on how to use the faucet
the construction punch list is shown in blue and yellow
the construction punch list is shown in blue and yellow
Setup and debug a Django app in PyCharm Community Edition #django #python
Setup and debug a Django app in PyCharm Community Edition #django #python
various types of pipe fittings are shown in this brochure for the best price
various types of pipe fittings are shown in this brochure for the best price
4 Projects To Take Your Web Scraping Skills To The Next Level
4 Projects To Take Your Web Scraping Skills To The Next Level
two round plastic containers sitting on top of each other
two round plastic containers sitting on top of each other

Python code for left join:

df_result = df1.join(df2, df1['student_id'] == df2['student_id'], 'left')

Right Outer Join

The right join is the opposite of the left join. It returns all the rows from the right DataFrame (df2) and the matching rows from the left DataFrame (df1). If no match is found, it includes null values.

Python code for right join:

df_result = df1.join(df2, df1['student_id'] == df2['student_id'], 'right')

Full Outer Join

The full outer join returns all the rows from both DataFrames and fills in null values for missing matches on either side.

Python code for full outer join:

df_result = df1.join(df2, df1['student_id'] == df2['student_id'], 'outer')

Using Join with Conditional Expressions

In some cases, you might want to join DataFrames based on multiple conditions or complex expressions. In such scenarios, use the when and otherwise functions from the PySpark functions module.

For example, let's join df1 and df2 based on student ID and age condition, (df1['age'] > df2['avg_age']).

Python code for join with conditional expression:

df_result = df1.alias('df1').join(df2.alias('df2'), (df1['student_id'] == df2['student_id']) & (df1['age'] > df2['avg_age']), 'inner')

In the real world, the choice of join type depends on the problem at hand and the characteristics of your data. Experiment with different join types and conditions to optimize your data processing workflows in PySpark.

Remember to explore other functionality provided by Spark SQL, such as distributed caching, statistics, and user-defined functions, to make the most of your data manipulation tasks.

Now that you're well-versed with PySpark join types, it's time to practice and apply these concepts to your own datasets. Keep honing your skills and stay updated with Apache Spark's latest developments to become an expert data engineer!