27. 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.Learn the concepts
- join · 5 min read. Combine DataFrames on a key: inner, left, right and full joins, and the duplicate-key fan-out trap.
- select · 6 min read. Choose, rename and compute columns, and why select beats a chain of withColumn calls.
- filter / where · 5 min read. Keep the rows that match a condition, and the null and operator traps that silently drop data.
PySpark functions you'll practise
- join
- filter
- select
Related problems
- Customers Without Orders · Medium · Joins, Filtering & Selection
- Employees and Their Departments · Medium · Joins, Filtering & Selection
- Year-over-Year Growth · Medium · Joins, Filtering & Selection
- Joining on Nullable Keys · Medium · Joins, Null Handling, Filtering & Selection
- Customers Who Bought Every Product · Hard · Joins, Aggregations, Filtering & Selection
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