Employees Earning More Than Their Managers Problem


Description

LeetCode Problem 181.

The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.

1
2
3
4
5
6
7
8
+----+-------+--------+-----------+
| Id | Name  | Salary | ManagerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | NULL      |
| 4  | Max   | 90000  | NULL      |
+----+-------+--------+-----------+

Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.

1
2
3
4
5
+----------+
| Employee |
+----------+
| Joe      |
+----------+


MySQL Solution

1
2
3
4
5
select a.name as employee
from Employee a
left join Employee b 
on (a.managerid = b.id)
where (a.salary > b.salary)




Related Posts

Nth Highest Salary Problem

LeetCode 177. Write a SQL query to get the nth...

Rank Scores Problem

LeetCode 178. Write a SQL query to rank scores. If...

Consecutive Numbers Problem

LeetCode 180. Write an SQL query to find all numbers...

Second Highest Salary Problem

LeetCode 176. Given the Employee table, write a SQL query...

Employees Earning More Than Their Managers Problem

LeetCode 181. Given the Employee table, write a SQL query...

Combine Two Tables Problem

LeetCode 175. Write a SQL query for a report that...