1.查询每个部门工资前三的员工
员工表
部门表
代码:
#select d.Name as Department,e.Name as Employee,e.Salary as Salary from Employee e,Department d where e.DepartmentId = d.Id
select bb.Name as Department,aa.Name as Employee,aa.Salary as Salary from
(
select * from Employee e
where exists(select count(*) from Employee em where em.Salary >= e.Salary
AND em.DepartmentId = e.DepartmentId group BY em.DepartmentId HAVING COUNT(DISTINCT em.Salary)<=3)
)
as aa,Department as bb
where aa.DepartmentId = bb.Id
order by Department,Salary desc
代码参实例2:
#mysql实现分组查询每个班级的前三名
select a.class,a.score from student a where (select count(*) from student where a.class=class and a.score<score)<3
order by a.class, a.score desc;