在oracle中 “查询并显示每个部门的最高工资、最低工资、并按照部门编号降序排列” 怎么搞?

select deptno, (select max(sal) from emp group by deptno) maxSal,(select min(sal) from emp group by deptno) minSal
from emp order by deptno desc 哪里错了?

错误原因是:
(select max(sal) from emp group by deptno) maxSa
(select min(sal) from emp group by deptno) minSal
group by deptno放错了位置,应该在这里去掉,在from emp 后面加上。
最好的写法是:
select deptno,max(sal) as maxSal,min(sal) as minSal from emp
group by deptno
order by deptno desc
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-07-31
(select max(sal) from emp group by deptno) maxSal,(select min(sal) from emp group by deptno),这两列里出来的最大最小值,未必是对应的deptno的,而是所有部门的最大最小值,
这么改:
select a.deptno, (select max(sal) from emp where deptno=a.deptno) maxSal,(select min(sal) from emp where deptno=a.deptno) minSal
from emp a order by a.deptno desc本回答被提问者采纳
第2个回答  2011-08-01
你不能那么写,为什么不直接应用函数
select deptno,max(sal) maxSal,min(sal) minSal from emp order by deptno desc;
第3个回答  2011-07-31
一句代码就叫搞定了:
select deptno, max(sal) maxSal,min(sal) minSal
from emp group by deptno order by deptno desc
相似回答