Inner Join
PySpark data engineering interview problem. Difficulty: beginner. Pattern: Joins. About 12 minutes. Free to practice.
Inner join orders to users on user_id. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
You have 2 tables. orders has order_id, user_id and amount. users has user_id, name and email. You want each order together with the name and email of the person who placed it. Use an inner join on user_id. An inner join keeps only the rows that have a match on both sides, so an order with a user that is not in users is left out. 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 that last line so result holds your joined table.
Requirements
- Join orders and users on user_id with how="inner".
- Select order_id, user_id, amount, name, email.
- Sort by order_id.
- Store the final DataFrame in result.
Constraints
- orders has 5 rows and users has 4 rows.
- Join with orders.join(users, on="user_id", how="inner"), which keeps one user_id column.
- Only orders whose user_id exists in users appear in the result.
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] 105 | 1 | 60.0 | Ada | [email protected] Order 104 has user_id 9 with no user row, so it drops.
Topics: lakebench, pyspark, inner.
More PySpark interview questions · All interview problems · Learn data engineering
Inner Join
Interview-style drill: Inner join orders to users on user_id.
Inner join `orders` to `users` on `user_id`. Select `order_id`, `user_id`, `amount`, `name`, `email`. Order by `order_id`. Assign `result`.