Oracle查询平均工资最高的部门信息

Kevin2_J 2017-11-28 07:44:46
select * from dept
where deptno=
(select e.d from(
select deptno d,avg(nvl(sal,0)) a,row_number() over(order by avg(nvl(sal,0)) desc) r from emp group by deptno ) e
where e.r=1) ;
用到的表emp、dept,这样写可以出来结果,感觉有点low,正在学习oracle,看大虾有没有更简洁的方法。
...全文
2062 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zbdzjx 2017-11-29
  • 打赏
  • 举报
回复
也可以这样:
select * from dept
where deptno=
(select e.d from(
select deptno d from emp group by deptno order by avg(nvl(sal,0)) desc) e
where rownum=1) ;
但感觉都差不多了,一个是用row_number(),一个没用row_number()。
碧水幽幽泉 2017-11-28
  • 打赏
  • 举报
回复
试试这个,手机打字,没有验证:

select a.*
  from dept a,
       (select deptno,row_number()over(order by avg(nvl(sal,0)) desc) rn from emp)b
 where a.deptno = b.deptno
   and b.rn = 1
acen_chen 2017-11-28
  • 打赏
  • 举报
回复
lower也不算lower,只是如果多个部门并列最高的时候不知道 你这sql符合要求否

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧