Joins, Unions, and Broadcast Joins
Databricks Spark Developer Associate · Lesson 11 of 18 · 1:28:34
YouTube loads only after you press play. This chapter opens at its timestamp in the full course. Watch on YouTube ↗
Learning with these videos? Subscribe for new courses and exam-style questions.
Subscribe on YouTube (opens in a new tab)In this lesson
Prepare for the Databricks Certified Associate Developer for Apache Spark exam. In this lesson: inner, outer, semi, and anti joins, joins on several keys and cross joins, union and unionByName, and broadcast joins. Includes three original practice questions.
Study notes
The text of this lesson, to read at your own pace. Information as of November 2026.
Purchases, players, and countries
Coralpeak's purchases must be matched with the player profiles, a small table of countries, and last month's archive. Some players have never bought anything, and the table of countries is tiny next to billions of purchases. This lesson covers inner and outer joins, several keys and cross joins, unions, and broadcast joins.
Inner and outer joins
join takes the other DataFrame, the join condition, and the join type, and an inner join is the default, which keeps only the rows that match on both sides. A left join, also called left outer, keeps every row from the left side, with nulls where the right side has no match, and right and full joins do the same for the right side, or for both. A left semi join keeps the left rows that have a match, without adding any column from the right, while a left anti join keeps the left rows that have no match at all. Passing the key as a column name, or as a list of names, gives a single key column in the result. Passing an expression instead, such as one column equal to another, keeps both key columns, so you then select or drop one of them.
Several keys and cross joins
To join on multiple keys, pass a list of column names, such as the player and the day, when the names are the same on both sides. When the names differ, combine the conditions with the ampersand, with parentheses around each one, exactly as in a filter. crossJoin returns the Cartesian product: every row on the left paired with every row on the right, so a thousand rows by a thousand rows give a million. It is useful for small lists, such as every country paired with every day, but on large tables, it makes the data explode in size.
Unions
union stacks the rows of two DataFrames with the same columns, and it matches the columns by position, not by name. It keeps the duplicates, like UNION ALL in SQL, and unionAll is simply another name for it, so call distinct afterwards to remove them. unionByName matches the columns by name instead, which is safer when the order of the columns differs. With allowMissingColumns set to true, a column that exists on one side only is filled with nulls on the other side.
Broadcast joins
A regular join between two large tables shuffles both sides, so that the matching keys meet in the same partition. A broadcast join copies the small table to every executor instead, so the large table stays where it is, with no shuffle at all. Spark chooses it on its own when a table is smaller than spark.sql.autoBroadcastJoinThreshold, ten megabytes by default, and a value of minus one turns this off. You can also ask for it with the broadcast function on the small DataFrame, the hint method, or a broadcast hint in SQL, and a hint wins over the threshold. Every executor gets a full copy, and the driver collects it first, so the broadcast table must fit in their memory, which only suits small tables.
Recap
Let's recap. join is an inner join by default, a left join keeps every left row, and a left anti join keeps the left rows with no match. Several keys go in a list of names, or in conditions joined by the ampersand, and crossJoin pairs every row with every row. union matches the columns by position and keeps the duplicates, like unionAll. unionByName matches by name, and allowMissingColumns fills the gaps with nulls. And a broadcast join copies a small table to every executor, on its own below ten megabytes, or on request with broadcast or a hint.
Quick checks for this lesson
The 3 questions of this lesson, as plain text. The button above runs them with shuffled choices and the reason behind every choice.
Marketing wants the players who have never made a purchase, with only the columns of the players table. Which join type fits?
- inner
- left
- left_semi
- left_anti
Show the answer
left_anti
A left anti join keeps the players with no match among the purchases, and only their columns. “inner” would keep only the players who did buy. “left” would keep every player, buyers included, with the purchase columns. And “left_semi” would keep the buyers, without adding any column.
The archive has the same columns as this month's events, but in a different order, and union puts amounts in the country column. What should the developer call instead?
- union(archive).distinct()
- unionByName(archive)
- unionAll(archive)
- crossJoin(archive)
Show the answer
unionByName(archive)
UnionByName matches the columns by name, whatever their order. “union(archive).distinct()” is wrong, since union still matches by position, and distinct does not fix the columns. “unionAll(archive)” is another name for union, with the same problem. And “crossJoin(archive)” would pair every row with every other row.
The plan of a join between billions of purchases and a small table of countries shows a shuffle of both sides. How can the developer avoid shuffling the purchases?
- Repartition the purchases by country first
- Cache the purchases before the join
- Wrap the countries in broadcast()
- Wrap the purchases in broadcast()
Show the answer
Wrap the countries in broadcast()
Broadcasting the small table copies it to every executor, so the purchases stay where they are. “Repartition the purchases by country first” would itself shuffle the purchases. “Cache the purchases before the join” would keep the purchases in memory, but the join would still shuffle them. And “Wrap the purchases in broadcast()” would try to copy billions of rows to every executor, which is the wrong side.