Skip to content

Joining and combining DataFrames with the different join and union types

Spark SQL and DataFrame APIs support standard SQL join types plus union operations for combining datasets. Join type and key selection affect result cardinality and null handling, while broadcast joins optimize performance when one side is small.

1 · Learn the must-know

  • Inner join returns only matching rows from both DataFrames; left (outer) join keeps all rows from the left side, filling unmatched right-side columns with null.
  • Multiple join keys are specified by passing a list of column names or an AND-combined boolean expression, e.g. df1.join(df2, ['col1','col2']) or df1.join(df2, (df1.a==df2.a) & (df1.b==df2.b)).
  • Broadcast join (broadcast() hint or automatic via spark.sql.autoBroadcastJoinThreshold) sends a small DataFrame to all executors to avoid a costly shuffle when joining with a much larger table.
  • Cross join produces the Cartesian product of two DataFrames (every row paired with every row) and can explode row counts quickly, so it must be used deliberately, often via crossJoin() or an explicit CROSS JOIN.
  • union() (or unionByName()) combines rows from two DataFrames with the same schema and does NOT remove duplicates, unlike SQL UNION which dedupes; use distinct() after union to mimic that behavior.
  • unionByName() matches columns by name rather than position, which avoids silently misaligned data when column order differs between DataFrames, and can optionally allow missing columns with allowMissingColumns=True.

2 · Check your understanding

Check this objectiveFree · always available

A table customers has columns customer_id and region, and a table subscriptions has columns customer_id, region, and plan. Some customers have moved regions over time, so a match should only occur when both customer_id AND region agree between the two tables. Which code correctly implements this requirement?

Your objective map0 tried · 0 answered correctly · 33 untouched

What you have tried across Databricks DEA's objectives, not a readiness score.

Databricks Intelligence Platform6% of the exam0 of 2 tried
Data Ingestion and Loading21% of the exam0 of 7 tried
Data Transformation and Modeling22% of the exam0 of 7 tried
Working with Lakeflow Jobs16% of the exam0 of 4 tried
Implementing CI/CD10% of the exam0 of 4 tried
Troubleshooting, Monitoring, and Optimization10% of the exam0 of 5 tried
Governance and Security15% of the exam0 of 4 tried

3 · Keep going