Bird
Raised Fist0
Apache Sparkdata~20 mins

Multi-column joins in Apache Spark - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Multi-column Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of multi-column join with inner join type
Given two Spark DataFrames df1 and df2 joined on columns id and category using inner join, what is the output of the following code?
Apache Spark
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
data1 = [(1, 'A', 100), (2, 'B', 200), (3, 'A', 300)]
data2 = [(1, 'A', 'X'), (2, 'B', 'Y'), (2, 'C', 'Z')]
df1 = spark.createDataFrame(data1, ['id', 'category', 'value1'])
df2 = spark.createDataFrame(data2, ['id', 'category', 'value2'])
joined_df = df1.join(df2, on=['id', 'category'], how='inner')
result = joined_df.collect()
A[Row(id=1, category='A', value1=100, value2='X'), Row(id=2, category='B', value1=200, value2='Y'), Row(id=2, category='C', value1=None, value2='Z')]
B[Row(id=1, category='A', value1=100, value2='X'), Row(id=2, category='B', value1=200, value2='Y')]
C[Row(id=1, category='A', value1=100, value2='X'), Row(id=3, category='A', value1=300, value2=None)]
D[Row(id=2, category='C', value1=None, value2='Z')]
Attempts:
2 left
💡 Hint
Inner join returns only rows with matching keys in both DataFrames.
❓ data_output
intermediate
1:30remaining
Number of rows after left join on multiple columns
If you perform a left join of df1 with df2 on columns id and category, how many rows will the resulting DataFrame have?
Apache Spark
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
data1 = [(1, 'A', 100), (2, 'B', 200), (3, 'A', 300)]
data2 = [(1, 'A', 'X'), (2, 'B', 'Y'), (2, 'C', 'Z')]
df1 = spark.createDataFrame(data1, ['id', 'category', 'value1'])
df2 = spark.createDataFrame(data2, ['id', 'category', 'value2'])
joined_df = df1.join(df2, on=['id', 'category'], how='left')
count = joined_df.count()
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Left join keeps all rows from the left DataFrame.
🔧 Debug
advanced
1:30remaining
Identify the error in multi-column join code
What error will this Spark code raise when trying to join two DataFrames on multiple columns?
Apache Spark
df1.join(df2, on='id,category', how='inner')
AAnalysisException: cannot resolve 'id,category' given input columns
BSyntaxError: invalid syntax
CNo error, join works correctly
DTypeError: join() argument 'on' must be a string or a list of strings
Attempts:
2 left
💡 Hint
The 'on' parameter expects a list of column names for multiple columns.
🚀 Application
advanced
1:30remaining
Choosing join type for multi-column join scenario
You have two DataFrames: sales with columns store_id, date, sales_amount and targets with columns store_id, date, target_amount. You want to keep all sales records and add target amounts where available. Which join type should you use on columns store_id and date?
AFull outer join
BInner join
CRight join
DLeft join
Attempts:
2 left
💡 Hint
You want to keep all sales records even if no target exists.
🧠 Conceptual
expert
1:30remaining
Effect of join keys order in multi-column join
In Spark DataFrame join, does the order of columns in the on list (e.g., ['id', 'category'] vs ['category', 'id']) affect the join result?
ANo, the order of join keys does not affect the join result
BNo, but it affects the performance of the join
CYes, the order changes the join keys and result rows
DYes, but only for outer joins, not inner joins
Attempts:
2 left
💡 Hint
Join keys are matched as a set, order does not matter for equality.

Practice

(1/5)
1.

What does a multi-column join in Apache Spark do?

Choose the best description.

easy
A. It sorts a DataFrame by multiple columns.
B. It merges two DataFrames by adding columns without any condition.
C. It filters rows from a single DataFrame based on multiple columns.
D. It joins two DataFrames using more than one column to match rows.

Solution

  1. Step 1: Understand join basics

    A join combines rows from two tables based on matching column values.
  2. Step 2: Recognize multi-column join

    Multi-column join uses more than one column to find matching rows, improving accuracy.
  3. Final Answer:

    It joins two DataFrames using more than one column to match rows. -> Option D
  4. Quick Check:

    Multi-column join = match on multiple columns [OK]
Hint: Multi-column join matches rows on several columns at once [OK]
Common Mistakes:
  • Confusing join with filtering
  • Thinking join adds columns without condition
  • Mixing join with sorting
2.

Which of the following is the correct syntax to perform a multi-column inner join between df1 and df2 on columns "id" and "date"?

df1.join(df2, _____, how="inner")
easy
A. ["id", "date"]
B. "id, date"
C. df1["id"] == df2["id"] and df1["date"] == df2["date"]
D. on=("id", "date")

Solution

  1. Step 1: Check join parameter for columns

    The on parameter accepts a list of column names for multi-column join.
  2. Step 2: Identify correct list syntax

    Using ["id", "date"] correctly passes both columns as a list.
  3. Final Answer:

    ["id", "date"] -> Option A
  4. Quick Check:

    List of columns = ["id", "date"] [OK]
Hint: Use a list of column names for multi-column join [OK]
Common Mistakes:
  • Passing columns as a single string
  • Using boolean expressions instead of list
  • Incorrect tuple syntax for columns
3.

Given the following two DataFrames:

df1 = spark.createDataFrame([
  (1, "2023-01-01", "A"),
  (2, "2023-01-02", "B"),
  (3, "2023-01-03", "C")
], ["id", "date", "value"])

df2 = spark.createDataFrame([
  (1, "2023-01-01", "X"),
  (2, "2023-01-04", "Y"),
  (3, "2023-01-03", "Z")
], ["id", "date", "value2"])

result = df1.join(df2, ["id", "date"], how="inner").select("id", "date", "value", "value2")
result.show()

What will be the output?

medium
A. +---+----------+-----+-------+ |id |date |value|value2 | +---+----------+-----+-------+ |1 |2023-01-01|A |X | |2 |2023-01-04|B |Y | |3 |2023-01-03|C |Z | +---+----------+-----+-------+
B. +---+----------+-----+-------+ |id |date |value|value2 | +---+----------+-----+-------+ |2 |2023-01-02|B |Y | +---+----------+-----+-------+
C. +---+----------+-----+-------+ |id |date |value|value2 | +---+----------+-----+-------+ |1 |2023-01-01|A |X | |3 |2023-01-03|C |Z | +---+----------+-----+-------+
D. +---+----------+-----+-------+ |id |date |value|value2 | +---+----------+-----+-------+ |1 |2023-01-01|A |null | |2 |2023-01-02|B |null | |3 |2023-01-03|C |Z | +---+----------+-----+-------+

Solution

  1. Step 1: Understand inner join on multiple columns

    Inner join keeps rows where both id and date match in both DataFrames.
  2. Step 2: Identify matching rows

    Rows with (1, "2023-01-01") and (3, "2023-01-03") match in both DataFrames; (2, "2023-01-02") and (2, "2023-01-04") do not.
  3. Final Answer:

    Rows with id 1 and 3 with matching dates and values -> Option C
  4. Quick Check:

    Inner join = only matching rows on all columns [OK]
Hint: Inner join keeps only rows matching all join columns [OK]
Common Mistakes:
  • Including rows with partial matches
  • Confusing inner join with outer join
  • Ignoring the multi-column condition
4.

Consider this code snippet:

result = df1.join(df2, on=["id", "date"], how="inner")

When running this, you get an error: TypeError: join() got an unexpected keyword argument 'on'. What is the likely cause?

medium
A. The how parameter must be a list, not a string.
B. The on parameter should be passed as the second positional argument, not as a keyword.
C. The join method does not support multi-column joins.
D. The DataFrames must have the same schema before joining.

Solution

  1. Step 1: Check join method signature

    In Spark, join() expects the join columns as the second positional argument, not as a keyword on.
  2. Step 2: Correct usage

    Use df1.join(df2, ["id", "date"], how="inner") without on=.
  3. Final Answer:

    The on parameter should be passed as the second positional argument, not as a keyword. -> Option B
  4. Quick Check:

    Join columns = positional argument [OK]
Hint: Pass join columns as positional argument, not 'on=' keyword [OK]
Common Mistakes:
  • Using 'on=' keyword in join
  • Passing 'how' as a list
  • Assuming join needs identical schemas
5.

You have two DataFrames, orders and customers. You want to join them on customer_id and region, but some customers have missing region values in one DataFrame. You want to keep all orders and add customer info where possible. Which join type and approach should you use?

hard
A. Use a left join on customer_id only, then filter or fill missing region values separately.
B. Use an inner join on both columns; only rows with matching customer_id and region appear.
C. Use a left join on both columns; missing region values will exclude some matches.
D. Use a full outer join on both columns to keep all rows from both DataFrames.

Solution

  1. Step 1: Understand join requirements

    You want all orders (left DataFrame) and customer info where available, but region may be missing in one DataFrame.
  2. Step 2: Choose join columns carefully

    Joining on both columns will exclude rows with missing region. Joining only on customer_id keeps all orders.
  3. Step 3: Handle missing region values separately

    After join, fill or filter missing region values as needed.
  4. Final Answer:

    Use a left join on customer_id only, then handle missing region values separately. -> Option A
  5. Quick Check:

    Left join on key column + handle missing data [OK]
Hint: Join on reliable keys first, handle missing columns after [OK]
Common Mistakes:
  • Joining on columns with missing values directly
  • Using inner join and losing rows
  • Using full outer join when not needed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard