编写一个SQL查询以查找每个部门中薪水最高的员工。 (代码184_Department_Highest_Salary)
有两个表:
- 员工(编号,姓名,薪水,部门编号),
- 部门(身份证,姓名)
我的回答正确,但是为什么不能使用一个选择而不是两个?
正确答案:
select d.name Department, e.name Employee, e.salary
from department d
join employee e
on d.id = e.departmentid
and salary >= (
select max(salary) from employee where departmentid=d.id)
我的想法:
select d.name Department, e.name Employee, max(e.salary) salary
from department d
join employee e
on d.id = e.departmentid
group by e.departmentid;
我希望他们一样吗?
我认为可以按DepartmentId
分组,每个组可以有一个MAX(salary)
。