Data Engineer Associate · Domain 3: Data Transformation and Modeling · 22% of the exam
Task 3.2: Combine DataFrames with operations such as Inner join, left join, broadcast join, multiple keys, cross join, union, and union all.
Join types and what each keeps, joining on several keys, broadcast hints, cross joins, and the difference between UNION, UNION ALL, union and unionByName.
Study it
Joins, broadcast joins, cross joins, union and union all
Try each one before opening the answer. Every option is explained, with the Databricks documentation page that proves it.
Question 1 · choose 1
The silver DataFrames orders and customers share the column customer_id. The gold report must list every order, with customer details where a customer exists and nulls where it does not. Which join gives this result?
A left semi join is also a filter on orders: the report would get no customer details, and orders without a customer would be missing.
The join type decides which unmatched rows survive. left (left_outer) keeps all left rows and fills nulls; inner keeps matches only; semi and anti joins filter the left side without adding the right side's columns.
Two DataFrames hold the same three columns in a different order: df_a has (id, name, city) and df_b has (id, city, name). The rows of both must be stacked into one DataFrame with each value under its own column. Which TWO approaches give the correct result? (Choose TWO.)
Adf_a.unionByName(df_b)
Bdf_a.union(df_b)
Cdf_a.join(df_b, 'id', 'outer')
Ddf_a.union(df_b.select('id', 'name', 'city'))
Edf_a.crossJoin(df_b)
Show the answer and why
Adf_a.unionByName(df_b)
Correct
unionByName resolves columns by name rather than position, so city and name line up correctly.
Bdf_a.union(df_b)
Incorrect
union resolves columns by position, so df_b's city values would land in name and its name values in city.
Cdf_a.join(df_b, 'id', 'outer')
Incorrect
A join matches rows on id and places the columns side by side; it does not stack rows.
Ddf_a.union(df_b.select('id', 'name', 'city'))
Correct
Selecting df_b's columns in df_a's order first makes the positional union line up, so each value lands under its own column.
Edf_a.crossJoin(df_b)
Incorrect
A cross join places the columns of the two DataFrames side by side instead of stacking their rows.
DataFrame union matches columns by position, while unionByName matches them by name. Either align the columns yourself before a union or let unionByName do it.
A 2 TB fact DataFrame sales is joined to a 60 MB dimension DataFrame stores on store_id. The 60 MB table is above the automatic broadcast threshold, so Spark shuffles both sides. The data engineer wants this one join to send stores to every executor instead. How should the join be written?
This asks for every pairing of rows and then filters them; it does not request a broadcast of stores.
Csales.join(stores, 'store_id', 'left_semi')
Incorrect
A left semi join returns only the sales columns, so the store attributes needed from the dimension are lost.
Dsales.join(broadcast(stores), 'store_id')
Correct
The broadcast function marks the small DataFrame for a broadcast join, so the large side is not shuffled.
Broadcast joins copy a small table to every executor so the large table can be joined in place. Spark broadcasts automatically below a size threshold; a hint or broadcast() requests it explicitly.
The DataFrames order_lines and shipments both identify a row by the pair (order_id, line_no). Each order line must be matched to its shipment on both columns. Which join does this?
A gold calendar grid needs one row for every combination of the 300 stores in stores and the 365 dates in dates, which share no columns. Which operation produces it?