题目来源:https://leetcode.com/problems/department-highest-salary/
题目意思是,将员工与部门联系起来,输出每个部门工资最高的员工。
我的想法不大优美,选择将员工与部门join起来通过员工的DepartmentId和部门的Id。然后记temp为不同部门id和它的最高工资。
最后算出哪些员工在此部门且他的工资为该部门最高工资:
select d.Name, e.Name, e.Salary
from Employee e join Department d on e.DepartmentId = d.Id,
(select DepartmentId tempId, max(Salary) Salary
from Employee
group by DepartmentId) temp
where e.DepartmentId = temp.tempId and e.Salary = temp.Salary