查询每个部门(deptname)中的工资(sal)前三名的人员姓名(name)

clipboard.png

SQL Server查询每个部门(deptname)中的工资(sal)前三名的人员姓名(name),这几个字段都在同一个表中table1

阅读 6.2k
2 个回答

如果是sql server:

select t.name from (
  select name,  Rank()
    over (PARTITION BY deptname ORDER BY sal desc) as Rank from table
  ) t where Rank <=3;

select name
from table1 t1
where
(

select count(1)     
from table1 t2     
where t2.deptname=t1.deptname and t2.sal>=t1.sal  

) <4
order by deptname, sal desc;

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题