您的位置:首页 > 其它

查询每个部门工资前三名的员工信息

2016-10-13 09:51 435 查看
问:Oracle的EMP表,查询每个部门工资前三名的员工信息,如何写?? 

解答:(通用sql)

[java] view
plain copy

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查询:(利用分区功能) 

[java] view
plain copy

select * from         

(select deptno,ename,sal,row_number() over (partition by deptno        

    order by sal desc) rn         

from emp)         

   where rn<3;
 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐