Left Join
PySpark data engineering interview problem. Difficulty: beginner. Pattern: Joins. About 12 minutes. Free to practice.
Left join orders to users so unmatched orders remain. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
Sometimes you want to keep every row from your main table, even if the other table has no match. A left join does that: all rows of the left table stay, and the columns from the right table are empty (None) when no match is found. Join orders (left) to users on user_id with a left join. Select order_id, user_id, amount, name and email, and sort by order_id. The editor already creates orders and users and ends with result = orders. Change the last line so result holds your joined table.
Requirements
- Use a left join with orders as the left table.
- Keep every order, even when its user does not exist.
- Select order_id, user_id, amount, name, email.
- Sort by order_id and store the DataFrame in result.
Constraints
- orders has 5 rows, so the result must have exactly 5 rows.
- Join with how="left" on user_id.
- An order with no matching user has None in name and email.
Examples
Input: orders + users Output: order_id | user_id | amount | name | email 101 | 1 | 50.0 | Ada | [email protected] 102 | 2 | 75.0 | Alan | [email protected] 103 | 3 | 20.0 | Grace | [email protected] 104 | 9 | 40.0 | None | None 105 | 1 | 60.0 | Ada | [email protected] Order 104 survives with null name/email.
Topics: lakebench, pyspark, left.
More PySpark interview questions · All interview problems · Learn data engineering
Left Join
Interview-style drill: Left join orders to users so unmatched orders remain.
Left join `orders` to `users` on `user_id`. Select `order_id`, `user_id`, `amount`, `name`, `email`. Order by `order_id`. Assign `result`.