LeetCode 181. Employees Earning More Than Their Managers SQL Solution
Problem
LeetCode SQL Problem
- Employees Earning More Than Their Managers
Employee Table
Id | Name | Salary | ManagerId |
---|---|---|---|
1 | Joe | 70000 | 3 |
2 | Henry | 80000 | 4 |
3 | Sam | 60000 | NULL |
4 | Max | 90000 | NULL |
Solution
- MySQL
- TSQL
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
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 |