mysql 收入超过他的经理的员工

SQL架构
Employee 表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。

+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
给定 Employee 表,编写一个 SQL 查询,该查询可以获取收入超过他们经理的员工的姓名。在上面的表格中,Joe 是唯一一个收入超过他的经理的员工。

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

SELECT NAME Employee
FROM
(
SELECT t2.name, t2.salary es, t1.salary ms
FROM employee t1 
JOIN employee t2 ON t1.id = t2.managerid
) t
WHERE t.es > t.ms

数据脚本:

create table `employee` (
	`id` int (11),
	`name` varchar (60),
	`salary` int (11),
	`managerid` int (11)
); 
insert into `employee` (`id`, `name`, `salary`, `managerid`) values('1','Joe','7000','3');
insert into `employee` (`id`, `name`, `salary`, `managerid`) values('2','Henry ','8000','4');
insert into `employee` (`id`, `name`, `salary`, `managerid`) values('3','Sam','6000',NULL);
insert into `employee` (`id`, `name`, `salary`, `managerid`) values('4','Max','9000',NULL);

猜你喜欢

转载自www.cnblogs.com/mengjianzhou/p/12801430.html