题目大概的意思是选出每个Department里工资最高的人的信息并组成相应的表信息
有几个值得注意的地方:1)使用group by语句时,前面的select语句后面的内容只能有两种情况一种是group by后面的属性,另一种是聚集函数。
2)在选取最大Salary时必须使用e1.Salary=e2.Salary and e1.DepartmentId=e2.DepartmentId两个条件,要不然会有重复。
基于这些考虑可以使用派生表查询来找出最大Salary,然后与Department表做自然连接。(最后的升序还是降序无所谓)
select dep.Name as Department, pans.Name as Employee, pans.Salary as Salary from Department dep, ( select e1.* from Employee e1, (select DepartmentId, max(Salary) as Salary from Employee group by DepartmentId) e2 where e1.Salary=e2.Salary and e1.DepartmentId=e2.DepartmentId ) pans where dep.Id=pans.DepartmentId order by pans.Salary desc;
LeetCode - Department Highest Salary
原文:http://www.cnblogs.com/wxisme/p/4442855.html