每天一道SQL题
每天一道SQL题之day06
1、题目:
链接: 超过经理收入的员工
表:Employee
Column Name | Type |
---|---|
id | int |
name | varchar |
salary | int |
managerId | int |
id 是该表的主键(具有唯一值的列)。
该表的每一行都表示雇员的ID、姓名、工资和经理的ID。
编写解决方案,找出收入比经理高的员工。
以 任意顺序 返回结果表。
结果格式如下所示。
示例 1:
输入:
Employee
表:
id | name | salary | managerId |
---|---|---|---|
1 | Joe | 70000 | 3 |
2 | Henry | 80000 | 4 |
3 | Sam | 60000 | Null |
4 | Max | 90000 | Null |
输出:
Employee |
---|
Joe |
2、题解
elect e1.name as 'Employee'
from Employee e1 ,Employee e2
where e1.managerId = e2.id and e1.salary > e2.salary;
3、解析
一张表内,就考虑自连接,自己和自己的条件进行比对。