[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 ,如需转载请自行联系原博主。

相关文章
|
2月前
|
算法 前端开发
1789. 员工的直属部门
1789. 员工的直属部门
19 0
|
2月前
|
SQL 算法 前端开发
1731. 每位经理的下属员工数量
1731. 每位经理的下属员工数量
11 0
|
3月前
|
SQL
leetcode-SQL-1731. 每位经理的下属员工数量
leetcode-SQL-1731. 每位经理的下属员工数量
18 0
|
11月前
【leetcode】690.员工的重要性
【leetcode】690.员工的重要性
37 0
|
SQL 算法
​LeetCode刷题实战184:部门工资最高的员工
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试。所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 !
115 0
​LeetCode刷题实战184:部门工资最高的员工
啥是好员工?
> 幸福的家庭都是相似的 不幸的家庭各有各的不幸 好员工都是相似的,不好的员工各有各的不好。好员工有哪些特征呢? # 1. 响应式而非命令式 计算机系统发展这么多年,一直在分层,就是为了封装复杂性,让调用者在不知道原理的前提下完成工作。每层的最终目标都是调用者只表达要求,不管具体实现。这就是所谓“响应式”,与之相反的则是“命令式”。“响应式”需要对原始问题高度抽象后表达成
1072 0