EasyNo. 0181

Employees Earning More Than Their Managers — LeetCode 181 Python Solution

Topics
Database
Reading time
2 min

The problem

List the names of the employees who earn strictly more than their own manager. The Employee table has id (int, primary key), name (varchar), salary (int) and managerId (int), which holds the id of another employee and is null for someone with no manager. Return the names in a column called Employee.

Example

Input
Employee(id, name, salary, managerId) = (1, 'Dana', 70000, 3), (2, 'Ravi', 80000, 4), (3, 'Sam', 60000, null), (4, 'Maya', 90000, null)
Output
Employee = 'Dana'
Explanation
Dana earns 70000 while her manager Sam earns 60000; Ravi earns less than his manager Maya.

Python solution

PythonSolution
import pandas as pd


def find_employees(employee: pd.DataFrame) -> pd.DataFrame:
    merged = employee.merge(
        employee, left_on="managerId", right_on="id", suffixes=("", "_manager")
    )
    result = merged[merged["salary"] > merged["salary_manager"]][["name"]]
    result.columns = ["Employee"]
    return result

Complexity

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0180Consecutive NumbersMedium
0182Duplicate EmailsEasy
0183Customers Who Never OrderEasy
0178Rank ScoresMedium
0184Department Highest SalaryMedium
0177Nth Highest SalaryMedium

Frequently asked questions

How hard is LeetCode 181. Employees Earning More Than Their Managers?

LeetCode 181. Employees Earning More Than Their Managers is rated Easy on LeetCode.

What topics does LeetCode 181. Employees Earning More Than Their Managers cover?

LeetCode 181. Employees Earning More Than Their Managers is tagged Database on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.