Featured Article

Master PySpark by Example: Ultimate Guide to Join Operations for Data Engineers

Kenneth Jul 13, 2026

Apache Spark, a robust open-source data processing engine, empowers data scientists and engineers to handle complex operations with ease. One such operation is data joining, which is pivotal in filtering and aggregating information from various sources. PySpark, the Python library for Apache Spark, facilitates this process with its user-friendly syntax. Let's explore PySpark by example with its join operations.

PySpark 4.0 Tutorial For Beginners with Examples - Spark By {Examples}
PySpark 4.0 Tutorial For Beginners with Examples - Spark By {Examples}

The join operation in PySpark is used to combine rows from two or more DataFrames, depending on a specified key column. Here, we'll discuss two common types of joins: inner joins and left joins.

PySpark Join Two or Multiple DataFrames
PySpark Join Two or Multiple DataFrames

Inner Join

An inner join returns records that have matching values in both tables (DataFrames, in PySpark). Let's consider two DataFrames, df1 and df2, with a common column 'key'.

PySpark Tutorial for Beginners
PySpark Tutorial for Beginners

Syntactically, an inner join can be performed as follows:

```python result = df1.join(df2, on='key', how='inner') ```

In the code snippet above, 'on' specifies the key column, while 'how' determines the type of join. 'inner' implies an inner join. The resulting DataFrame, 'result', will contain only the records where 'key' is present in both DataFrames.

How to set up PySpark for your Jupyter notebook
How to set up PySpark for your Jupyter notebook

Inner Join Example

Let's create two simple DataFrames for demonstration. df1 has columns ['key', 'value1'], and df2 has columns ['key', 'value2'].

df1 = spark.createDataFrame([(1,'a'), (2,'b'), (3,'c')], ['key', 'value1'])
df2 = spark.createDataFrame([(1,'x'), (2,'y'), (4,'z')], ['key', 'value2'])

The join operation would yield:

Learning PySpark
Learning PySpark

```python result.show() ``` ``` +---+-------+-------+ | key| value1| value2| +---+-------+-------+ | 1| a| x| | 2| b| y| +---+-------+-------+ ```

Left Join

A left join returns all the records from the left DataFrame (df1) and matching records from the right DataFrame (df2). If there is no match for a record in df2, then the right side of the result contains 'null'.

```python result = df1.join(df2, on='key', how='left') ```

In the result DataFrame, all 'key' values from df1 will be present, whether they match with df2 or not. Let's see this in action.

design
design

Left Join Example

```python result = df1.join(df2, on='key', how='left') result.show() ``` ``` +---+-------+-------+ | key| value1| value2| +---+-------+-------+ | 1| a| x| | 2| b| y| | 3| c| null| +---+-------+-------+ ```

As seen above, the record with 'key' = 3 from df1 does not have a match in df2, hence 'value2' is 'null'.

#tcs #dataengineer #dataengineering #pyspark #azuredatafactory #deltalake #sql #bigdata #interviewpreparation #careergrowth #techcareers | Kushboo Chauhan
#tcs #dataengineer #dataengineering #pyspark #azuredatafactory #deltalake #sql #bigdata #interviewpreparation #careergrowth #techcareers | Kushboo Chauhan
Mastering PySpark: Practical and Objective Guide
Mastering PySpark: Practical and Objective Guide
SQL Cheat Sheet for Beginners
SQL Cheat Sheet for Beginners
Marketplace Pitch Deck - How To Instructions
Marketplace Pitch Deck - How To Instructions
Advanced Apache Spark for Data Scientists and Developers
Advanced Apache Spark for Data Scientists and Developers
pink handler set · preview
pink handler set · preview
Thank you for downloading PyCharm!
Thank you for downloading PyCharm!
Join Us In Speech Bubble Style, Join Us Sign, Sign, Join Us Banner PNG and Vector with Transparent Background for Free Download
Join Us In Speech Bubble Style, Join Us Sign, Sign, Join Us Banner PNG and Vector with Transparent Background for Free Download
Sponsorship Leaflet Images – Browse 5,444 Stock Photos, Vectors, and Video
Sponsorship Leaflet Images – Browse 5,444 Stock Photos, Vectors, and Video

Now that you've seen how to perform basic joins in PySpark, you're ready to apply this knowledge to your data processing tasks. The seamless integration of PySpark with Python makes it an excellent choice for working with large datasets. Happy coding!