sql hard
select d.name Department, e.name Employee, e.Salaryfrom employee e left join department d on e.departmentid = d.idwhere e.id in(select e1.id from employee e1 left join employee e2on e1.departmentid = e2.departmentid and e1.salary < e2.salarygroup by e1.idhaving count(distinct e2.salary) <= 2)and e.departmentid in (select id from department)order by d.id asc, e.salary desc;
笔记
能用基本语法就用基本语法, 能不用函数就不用函数;
这是一个 分组取前几名 的问题, 可以先 group by 然后用 having count() , 在这里, 先用了一个子查询, 查询出比自己工资高的最多只有 2 个的员工的 id, 那么, 这些个 id 就是各个部门中的薪资前三名.
