[LeetCode] Employees Earning More Than Their Managers 员工挣得比经理多

简介:

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

+----+-------+--------+-----------+
| 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.

+----------+
| Employee |
+----------+
| Joe      |
+----------+

这道题给我们了一个Employee表,里面有员工的薪水信息和其经理的信息,经理也属于员工,其经理Id为空,让我们找出薪水比其经理高的员工,那么就是一个很简单的比较问题了,我们可以生成两个实例对象进行内交通过ManagerId和Id,然后限制条件是一个Salary大于另一个即可:

解法一:

SELECT e1.Name FROM Employee e1
JOIN Employee e2 ON e1.ManagerId = e2.Id
WHERE e1.Salary > e2.Salary;

我们也可以不用Join,直接把条件都写到where里也行:

解法二:

SELECT e1.Name FROM Employee e1, Employee e2
WHERE e1.ManagerId = e2.Id AND e1.Salary > e2.Salary;

本文转自博客园Grandyang的博客,原文链接:员工挣得比经理多[LeetCode] Employees Earning More Than Their Managers ,如需转载请自行联系原博主。

相关文章
|
4月前
|
SQL
leetcode-SQL-570. 至少有5名直接下属的经理
leetcode-SQL-570. 至少有5名直接下属的经理
22 0
|
4月前
|
SQL
leetcode-SQL-181. 超过经理收入的员工
leetcode-SQL-181. 超过经理收入的员工
24 0
|
4月前
|
SQL
leetcode-SQL-1731. 每位经理的下属员工数量
leetcode-SQL-1731. 每位经理的下属员工数量
19 0
|
5月前
|
SQL 算法 vr&ar
☆打卡算法☆LeetCode 181. 超过经理收入的员工 算法解析
☆打卡算法☆LeetCode 181. 超过经理收入的员工 算法解析
|
数据库
LeetCode(数据库)- 上级经理已离职的公司员工
LeetCode(数据库)- 上级经理已离职的公司员工
101 0
|
数据库
LeetCode(数据库)- 每位经理的下属员工数量
LeetCode(数据库)- 每位经理的下属员工数量
69 0
|
数据库
LeetCode(数据库)- 仓库经理
LeetCode(数据库)- 仓库经理
81 0
|
数据库
LeetCode(数据库)- 至少有5名直接下属的经理
LeetCode(数据库)- 至少有5名直接下属的经理
83 0
|
数据库
LeetCode(数据库)- 超过经理收入的员工
LeetCode(数据库)- 超过经理收入的员工
77 0
|
SQL 算法
​LeetCode刷题实战181: 超过经理收入的员工
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
111 0
​LeetCode刷题实战181: 超过经理收入的员工

热门文章

最新文章