Skip to content

25. Employees and Their Departments

Difficulty: Medium · Topics: Joins, Filtering & Selection

Join employees with departments on dept_id.

Return name and dept_name for every employee. If an employee has no department (null dept_id or an id that doesn't exist), dept_name must be null.

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

employees

emp_idnamedept_id
1Asha10
2Ben20
3Chitranull
4Dev30

departments

dept_iddept_name
10Engineering
20Sales

Expected output

namedept_name
AshaEngineering
BenSales
Chitranull
Devnull

Hints

Hint 1An inner join would drop employees without a department.
Hint 2Use how="left" so every employee row is kept.
Hint 3Joining on a column name (string) avoids ambiguous duplicate dept_id columns.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance

Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice