Oracle的EMP表,查询每个部门工资前三名的员工信息,如何写??
解答:(通用sql)
select deptno, ename, sal
- from emp e1
- where
- (
- select count(1)
- from emp e2
- where e2.deptno=e1.deptno and e2.sal>=e1.sal
- ) <=3 /*这里的数值表示你想取前几名*/
- order by deptno, sal desc;
Oracle查询:(利用分区功能)
select * from
- (select deptno,ename,sal,row_number() over (partition by deptno
- order by sal desc) rn
- from emp)
- where rn<3;