Joins help combine two tables based on matching data. This lets you see related information together.
Inner, left, right, and full outer joins in Apache Spark
df1.join(df2, on='key_column', how='join_type')
df1 and df2 are the two tables (DataFrames) to join.
on is the column name(s) to match rows.
how is the join type: 'inner', 'left', 'right', or 'full'.
id matches in both tables.df1.join(df2, on='id', how='inner')
df1, with matching rows from df2 or null if no match.df1.join(df2, on='id', how='left')
df2, with matching rows from df1 or null if no match.df1.join(df2, on='id', how='right')
df1.join(df2, on='id', how='full')
This code creates two small tables: one with people and one with departments. It shows how to join them in four ways and prints the results.
from pyspark.sql import SparkSession spark = SparkSession.builder.appName('JoinExample').getOrCreate() # Create first DataFrame data1 = [(1, 'Alice'), (2, 'Bob'), (3, 'Charlie')] df1 = spark.createDataFrame(data1, ['id', 'name']) # Create second DataFrame data2 = [(2, 'Sales'), (3, 'HR'), (4, 'IT')] df2 = spark.createDataFrame(data2, ['id', 'department']) # Inner join inner_join = df1.join(df2, on='id', how='inner') print('Inner Join Result:') inner_join.show() # Left join left_join = df1.join(df2, on='id', how='left') print('Left Join Result:') left_join.show() # Right join right_join = df1.join(df2, on='id', how='right') print('Right Join Result:') right_join.show() # Full outer join full_join = df1.join(df2, on='id', how='fullouter') print('Full Outer Join Result:') full_join.show() spark.stop()
Inner join keeps only rows with matching keys in both tables.
Left join keeps all rows from the first table, adding nulls if no match in second.
Right join keeps all rows from the second table, adding nulls if no match in first.
Full outer join keeps all rows from both tables, filling nulls where no match.
Joins combine tables based on matching columns.
Inner join shows only matching rows.
Left, right, and full joins keep unmatched rows from one or both tables.
Practice
Solution
Step 1: Understand join types
Inner join returns rows where keys match in both tables only.Step 2: Compare with other joins
Left, right, and full outer joins include unmatched rows from one or both tables.Final Answer:
Inner join -> Option BQuick Check:
Inner join = matching rows only [OK]
- Confusing inner join with left or right join
- Thinking full outer join returns only matches
- Assuming left join excludes unmatched rows
df1 and df2 on column id in Apache Spark?Solution
Step 1: Identify join type keyword
Left outer join uses 'left' as the join type string in Spark.Step 2: Match syntax format
The syntax is df1.join(df2, condition, 'left').Final Answer:
df1.join(df2, df1.id == df2.id, 'left') -> Option DQuick Check:
Left join keyword = 'left' [OK]
- Using 'left_outer' instead of 'left'
- Confusing join type strings like 'right_outer' for left join
- Omitting join condition or using wrong syntax
df1 = spark.createDataFrame([(1, 'A'), (2, 'B')], ['id', 'val1'])df2 = spark.createDataFrame([(2, 'X'), (3, 'Y')], ['id', 'val2'])What will be the result of
df1.join(df2, 'id', 'right')?Solution
Step 1: Understand right join behavior
Right join keeps all rows from right DataFrame (df2) and matches from left (df1).Step 2: Match rows by 'id'
Id=2 matches in both, id=3 only in df2, so df1 columns are null for id=3.Final Answer:
[ (1, 'A', null), (2, 'B', 'X'), (3, null, 'Y') ] -> Option AQuick Check:
Right join keeps all right rows [OK]
- Expecting left table rows to appear in right join
- Confusing right join with full outer join
- Misunderstanding null placement for unmatched rows
df1.join(df2, df1.id == df2.id, 'full_outerjoin')But it throws an error. What is the likely cause?
Solution
Step 1: Check valid join type strings in Spark
Spark expects 'full' or 'fullouter' but not 'full_outerjoin'.Step 2: Identify correct string for full outer join
The correct string is 'full', so 'full_outerjoin' causes error.Final Answer:
The join type string should be 'full' instead of 'full_outerjoin' -> Option AQuick Check:
Use 'full' for full outer join [OK]
- Using 'full_outerjoin' instead of 'full'
- Thinking join condition must be string only
- Believing Spark lacks full outer join support
df1 = spark.createDataFrame([(1, 'A'), (2, 'B'), (4, 'D')], ['id', 'val1'])df2 = spark.createDataFrame([(2, 'X'), (3, 'Y'), (4, 'Z')], ['id', 'val2'])You want a DataFrame that shows all ids from both tables, with values from both, and
null where no match exists. Which join should you use and what will be the output?Solution
Step 1: Identify join type for all rows from both tables
Full outer join returns all rows from both DataFrames, filling nulls where no match.Step 2: Match rows and fill nulls
Ids 1 and 3 appear only in one table, so their unmatched columns are null.Final Answer:
Use full outer join; output: [(1, 'A', null), (2, 'B', 'X'), (3, null, 'Y'), (4, 'D', 'Z')] -> Option CQuick Check:
Full outer join = all rows, nulls for no match [OK]
- Using inner join and missing unmatched rows
- Confusing left or right join with full outer join
- Expecting no nulls in full outer join result
Ready to prove it?
Pass the test and this pattern moves unknown
