Employees who earn more than their manager
employee has columns id, name, salary, managerId (a self-reference: a row's managerId is the id of another row). Return a DataFrame of the names of employees whose salary is strictly greater than their manager's salary.
import pandas as pd
def higher_than_manager(employee: pd.DataFrame) -> pd.DataFrame:
# your code here
Write the implementation.
Self-merge the table on managerId == id to put each employee beside their manager, then filter where the employee's salary exceeds the manager's. The join key links each row's manager to that manager's own row so the two salaries sit side by side.
- ✗Comparing to the team or global average instead of the manager
- ✗Forgetting to self-join to bring the manager's salary alongside
- ✗Assuming only one report per manager can out-earn them
- →Why does the join use left_on managerId and right_on id?
- →What happens to employees whose managerId is null?
Bring each employee's manager onto the same row with a self-merge, then compare salaries.
import pandas as pd
def higher_than_manager(employee: pd.DataFrame) -> pd.DataFrame:
merged = employee.merge(
employee, left_on='managerId', right_on='id', suffixes=('_e', '_m'))
out = merged[merged['salary_e'] > merged['salary_m']]
return out[['name_e']].rename(columns={'name_e': 'name'})
The merge matches each employee row's managerId to the id of their manager's row, so salary_e and salary_m sit side by side. Employees with a null managerId have no match in an inner merge and are correctly excluded.