Skip to content

36. Employees Earning More than Their Manager

Difficulty: Medium · Topics: Joins, Filtering & Selection

employees has id, name, salary and manager_id (the id of their manager; null for the CEO).

Return employee and manager (names) for every employee who earns strictly more than their manager.

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

Sample data

employees

idnamesalarymanager_id
1Ceo300null
2Ann3201
3Bob1501
4Cat1603
5Dan1403

Expected output

employeemanager
AnnCeo
CatBob

Hints

Hint 1Join the table to itself: once as employees, once as managers.
Hint 2Give each side an alias (employees.alias("e")) so you can refer to e.salary and m.salary.
Hint 3Join on e.manager_id == m.id; the CEO drops out naturally in an inner join.

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

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · All problems