Skip to main content

LeetCode 181. Employees Earning More Than Their Managers SQL Solution

Problem

LeetCode SQL Problem

  1. Employees Earning More Than Their Managers

Employee Table

IdNameSalaryManagerId
1Joe700003
2Henry800004
3Sam60000NULL
4Max90000NULL

Solution

Return Employees Earning More Than Their Managers
SELECT EMP.Name
FROM Employee AS EMP -- Self Join Employee table
INNER JOIN Employee AS MGR ON EMP.ManagerId = MGR.Id
WHERE EMP.ManagerId IS NOT NULL -- Filter out employees that have no manager
AND EMP.Salary > MGR.Salary -- Employee's salary is greater than his/her manager

Query Output

Name
Joe