LeetCode 181- Employees Earning More Than Their Managers (SQL & Python) Solutions

Table of Contents

This article is going to provide the solution to LeetCode 181 Employees Earning More Than Their Managers. It’s an easy difficulty question.

Write a solution to find the employees who earn more than their managers.

Return the result table in any order.

The result format is in the following example.

LeetCode Question Link

If you want to skip the article, check out the full YouTube video where I code the answer live.

LeetCode 181 SQL Solution

				
					select e1.name as Employee
from Employee E1
left join Employee E2 on E2.id = E1.managerId
where e2.salary < e1.salary



				
			

The first part of the code selects the name of the employees from the Employee table (E1) and aliases it as Employee

Next perform a left join between the Employee table (E1) and itself (aliased as E2) based on the condition that the id of E2 matches the managerId of E1. This join is used to connect each employee with their respective manager.

Lastly, filter the joined result set to only include rows where the salary of the manager (E2.salary) is less than the salary of the employee (E1.salary).

LeetCode 181 Python Solution

				
					
def find_employees(employee: pd.DataFrame) -> pd.DataFrame:
    results = pd.merge(employee, employee, left_on='managerId', right_on='id', suffixes=('_employee', '_manager'))
    results2 = results[results['salary_employee'] > results['salary_manager']]
    results2.rename(columns={'name_employee': 'Employee'}, inplace=True)
    return results2[['Employee']]
				
			

LeetCode gives us a defined  function named find_employees that takes a pandas DataFrame named employee as input and returns another pandas DataFrame.

The first line merges the employee DataFrame with itself based on two columns: managerId from the left DataFrame (aliased as _employee) and id from the right DataFrame (aliased as _manager). The result is stored in the results DataFrame.

Next, filter the results DataFrame to include only rows where the salary of the employee is greater than the salary of their manager. The filtered result is stored in results2

Rename the column name_employee to Employee.

Lastly, return the Employee column from the results2 DataFrame

Free Community

Join 1,000+ AI Automation Builders

Weekly tutorials, live calls & direct access to Ryan & Matt.

Join Free →

Keep Learning

LeetCode 180- Consecutive Numbers (SQL & Python) Solutions

This article is going to provide the solution to LeetCode 180 Consecutive Numbers. It's an easy difficulty question. Find all numbers that...

LeetCode 178- Rank Scores (SQL & Python) Solutions

This article is going to provide the solution to LeetCode 178 Rank Scores. It's a medium difficulty question. Write a solution to...

LeetCode 607 - Sales Person (SQL & Python) Solutions

This article is going to provide the solution to LeetCode 607 Sales Person. It's an easy difficulty question. Write a solution to...

LeetCode 184 - Department Highest Salary (SQL & Python) Solutions

This article is going to provide the solution to LeetCode 184 Department Highest Salary. It's a medium difficulty question. Write a solution...