目录
1、184部门工资最高的员工
编写SQL查询以查找每个部门中薪资最高的员工。按任意顺序返回结果表。
使用join和in语句:
因为 Employee 表包含 Salary 和 DepartmentId 字段,我们可以以此在部门内查询最高工资。因为有可能有多个员工同时拥有最高工资,所以最好在这个查询中不包含雇员名字的信息。然后,我们可以把表 Employee 和 Department 连接,再在这张临时表里用 IN
语句查询部门名字和工资的关系。
select Department.name as 'Department'
,Employee.name as 'Employee'
,salary as 'Salary' from Employee
inner join Department on departmentId=Department.id
where (departmentId,salary)
in (select departmentId,max(salary)
from Employee
group by departmentId)
2、185部门工资前三高的全部成员
公司的主管们感兴趣的是公司每个部门中谁赚的钱最多。一个部门的 高收入者 是指一个员工的工资在该部门的 不同 工资中 排名前三 。
编写一个SQL查询,找出每个部门中 收入高的员工 。以 任意顺序 返回结果表。
Create table If Not Exists Department (id int, name varchar(255));
Truncate table Employee2;
insert into Employee2 (id, name, salary, departmentId) values ('1', 'Joe', '85000', '1');
insert into Empl