Skip to content

Combining datasets with join, union, intersect, and except

Azure Databricks supports SQL-style set and join operators in both PySpark DataFrame API and Spark SQL to combine or compare datasets. Joins merge columns from two DataFrames based on matching keys, while union, intersect, and except combine or compare rows across DataFrames with matching schemas.

1 · Learn the must-know

  • DataFrame joins (join()) support types like inner, left, right, outer/full, left_semi, and left_anti, specified via the 'how' parameter, mirroring standard SQL JOIN semantics.
  • union() combines rows from two DataFrames by position (not by column name) and requires both DataFrames to have the same number of columns with compatible data types; use unionByName() to align columns by name instead, which also supports an allowMissingColumns option.
  • unlike SQL UNION, PySpark's union() does not remove duplicates by default—chain .distinct() after union() to deduplicate rows.
  • intersect() returns only the distinct rows present in both DataFrames, while intersectAll() preserves duplicates by returning matching row counts based on multiplicity.
  • exceptAll() (or subtract() in older APIs) returns rows in the first DataFrame not present in the second, respecting duplicate counts, whereas except()/exceptAll() differ in duplicate-handling similarly to intersect vs intersectAll.
  • left_semi and left_anti joins are efficient alternatives to intersect and except for filtering rows in one DataFrame based on presence or absence of matches in another, without duplicating or bringing in columns from the right DataFrame.

2 · Check your understanding

Check this objectiveFree · always available

A data engineer at an Azure Databricks workspace runs the following Spark SQL query to combine sales records from two regional tables that share the same schema: SELECT order_id, amount FROM west_sales UNION SELECT order_id, amount FROM east_sales. The engineer expected the result to contain every row from both tables, including rows where an order_id and amount pair happens to repeat across the two tables. Instead, some rows appear to be missing from the output. What is the most likely cause?

Your objective map0 tried · 0 answered correctly · 77 untouched

What you have tried across DP-750's objectives, not a readiness score.

Set up and configure an Azure Databricks environment15-20% of the exam0 of 13 tried
Secure and govern Unity Catalog objects15-20% of the exam0 of 12 tried
Prepare and process data30-35% of the exam0 of 28 tried
Deploy and maintain data pipelines and workloads30-35% of the exam0 of 24 tried

3 · Keep going