185. 部门工资前三高的所有员工
1 # Write your MySQL query statement below 2 select d.name Department, e1.Name Employee, e1.Salary from Employee e1 left join Employee e2 3 on e1.DepartmentId = e2.DepartmentId and e1.Salary < e2.Salary 4 join Department d on e1.DepartmentId = d.Id 5 group by e1.Id having count(distinct e2.Salary) <=2 order by e1.Salary desc;
首先使用左连接,进行第一次筛选,选出比当前的工资高的数据
然后再和department进行inner join一次,删除不合法的数据
然后再就是调用group by,进行分组以便于查询
然后分组之后,判断个数即可,如果大于当前值的不重复的有两个,就算符合规则