超过经理收入的员工

发布时间 2023-07-26 11:56:52作者: 网抑云黑胶SVIP用户
表:Employee 

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| name        | varchar |
| salary      | int     |
| managerId   | int     |
+-------------+---------+
Id是该表的主键。
该表的每一行都表示雇员的ID、姓名、工资和经理的ID。
 

编写一个SQL查询来查找收入比经理高的员工。

以 任意顺序 返回结果表。

查询结果格式如下所示。

 

示例 1:

输入: 
Employee 表:
+----+-------+--------+-----------+
| id | name  | salary | managerId |
+----+-------+--------+-----------+
| 1  | Joe   | 70000  | 3         |
| 2  | Henry | 80000  | 4         |
| 3  | Sam   | 60000  | Null      |
| 4  | Max   | 90000  | Null      |
+----+-------+--------+-----------+
输出: 
+----------+
| Employee |
+----------+
| Joe      |
+----------+
解释: Joe 是唯一挣得比经理多的雇员。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/employees-earning-more-than-their-managers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

根据题目意思,首先我们要进行自链接,通过managerId和id键
select * from Employee e1
inner join Employee e2
on e1.managerId = e2.id 


按题目意思直接筛选出符合条件的员工名
select e1.name as Employee  from Employee e1
inner join Employee e2
on e1.managerId = e2.id 
and e1.salary > e2.salary