181.Employees Earning More Than Their Managers
1、题目与答案
The
Employee
table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
表Employee
中包含包括管理者在内的所有员工,每一个员工都有一个id,还有一列表示该员工的管理者的 id。
Table:Employees
:
+----+-------+--------+-----------+
| Id | Name | Salary | ManagerId |
+----+-------+--------+-----------+
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |
+----+-------+--------+-----------+
Given the Employee table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
写出SQL语句,找出收入比其管理者还多的员工,在上面的那张表中,Joe就是唯一符合条件的。
+----------+
| Employee |
+----------+
| Joe |
+----------+
答案:
select distinct em1.name as Employee
from employee em1,employee em2
where em1.managerId=em2.id and em1.salary > em2.salary;
2、知识点总结
验证步骤:
create table employee(
id int primary key not NULL,
name varchar(50) ,
salary int ,
managerId int
);
insert into employee()
values
(1,"Joe",70000,3),
(2,"Henry",80000,4),
(3,"Sam",60000,NULL),
(4,"Max",90000,NULL);
知识点:
select ... as
作为列名where
条件筛选