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
| id | name | salary | manager_id |
|---|---|---|---|
| 1 | Ceo | 300 | null |
| 2 | Ann | 320 | 1 |
| 3 | Bob | 150 | 1 |
| 4 | Cat | 160 | 3 |
| 5 | Dan | 140 | 3 |
Expected output
| employee | manager |
|---|---|
| Ann | Ceo |
| Cat | Bob |
Hints
Hint 1
Join the table to itself: once as employees, once as managers.Hint 2
Give each side an alias (employees.alias("e")) so you can refer to e.salary and m.salary.Hint 3
Join one.manager_id == m.id; the CEO drops out naturally in an inner join.PySpark functions you'll practise
- join
- filter
- select
Related problems
- Customers Who Bought Every Product · Hard · Joins
- Monthly Retention by Cohort · Hard · Joins
- Customers Without Orders · Medium · Joins
- Employees and Their Departments · Medium · Joins
- Year-over-Year Growth · Medium · Joins
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