181.找到收入比经理高的员工
建表插入数据:
Create table If Not Exists Employee (id int, name varchar(255), salary int, managerId varchar(10));
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');
思路分析:
找收入比经理高的员工,需要拿到每一个人的工资salary和经理的工资salary进行对比,因此需要自连接,连接的条件就是e1.managerid=e2.id,找出e1.salary>e2.salary的人即可。
画图分析:
代码实现:
#181.找出收入比经理高的员工
select e1.name as Employee from employee e1 join employee e2 on e1.managerId=e2.id where e1.salary>e2.salary;