> 文章列表 > SQL 181. 超过经理收入的员工

SQL 181. 超过经理收入的员工

SQL 181. 超过经理收入的员工

SQL 181. 超过经理收入员工

  • 数据
  • 需求
  • 解决

题目 : https://leetcode.cn/problems/employees-earning-more-than-their-managers/

数据

Create table If Not Exists Employee (id int , name varchar(255) comment '姓名', salary int comment '工资', managerId int comment '经理的ID'
);Truncate table Employee;insert into Employee (id, name, salary, managerId) values (1, 'Joe', 70000, 3);
insert into Employee (id, name, salary, managerId) values (2, 'Henry', 80000, 4);
insert into Employee (id, name, salary, managerId) values (3, 'Sam', 60000, null);
insert into Employee (id, name, salary, managerId) values (4, 'Max', 90000, null);

需求

查询来查找收入比经理高的员工

查询结果 :

| Employee |
| Joe      |

解决

思路:

  • 把一张表当员工表,其中员工表有经理id
  • 把一张表当经理表
  • 把员工表的经理id 连接经理表 id
  • 用员工钱比较经理的钱即可
select t2.name as Employee
from Employee t1 join Employee t2on t1.id = t2.managerId
where t2.salary > t1.salary;