LeetCode 185. Department Top Three Salaries

select c.Name, a.Name, a.salary from 
Employee a,
(
    select ifnull(b.c,0) as c,a.Id from 
    Employee a left join
    (
        select count(1) as c,a.Id from Employee a,(select DepartmentId,salary from Employee b group by DepartmentId,salary) b
        where a.Salary < b.Salary and a.DepartmentId = b.DepartmentId 
        group by a.Id
    ) b on a.Id = b.Id
) b,
Department c 
where a.DepartmentId = c.Id and a.Id = b.Id and b.c < 3

思路:
需要先取出工资中不重复的工资,我用的group by
在选择分组前几的时候需要查询两张相同表的关联

你可能感兴趣的:(LeetCode 185. Department Top Three Salaries)