Oracle單表的復雜查詢
?
select avg (sal), max (sal),deptno from emp group by deptno;
select avg (sal), max (sal),deptno from emp group by deptno having avg (sal)> 2000 order by deptno;
?
查詢工資高于 500 或者是崗位為 MANAGER 的雇員,同時還要滿足他們的姓名首字母為大寫的 J ?
select * from emp where (sal> 500 or job= 'manager' ) and ename like 'J%' ;
按照部門號升序而雇員的工資降序排列
select * from emp order by deptno asc , sal desc ;
select (sal+ nvl (comm, 0 ))* 12 as sum ,ename from emp order by sum ;
求最高工資和最低工資
select max (sal), min (sal) from emp ;
查詢最高工資員工的名字,工作崗位
select ename,sal from emp where ( select max (sal) from emp )=sal;
顯示工資高于平均工資的員工信息
select * from emp where sal>( select avg (sal) from emp);
group by
和
having
子句
group by
用于對查詢的結果分組統(tǒng)計,
having
子句用于限制分組顯示結果。
如何顯示每個部門的平均工資和最高工資
select avg (sal), max (sal),deptno from emp group by deptno;
顯示每個部門的每種崗位的平均工資和最低工資?
select avg (sal), max (sal), min (sal),deptno,job from emp group by deptno,job;
顯示平均工資低于 2000 的部門號和它的平均工資?
select avg (sal), max (sal),deptno from emp group by deptno having avg (sal)> 2000 order by deptno;
對數(shù)據(jù)分組的總結:
1
分組函數(shù)只能出現(xiàn)在選擇列表、
having
、
order by
子句中
(
不能出現(xiàn)在
where
中
)
2
如果在
select
語句中同時包含有
group by, having, order by
那么它們的順序是
group by, having, order by
3
在選擇列中如果有列、表達式和分組函數(shù),那么這些列和表達式必須有一個出現(xiàn)在
group by
子句中,否則就會出錯。
?? 如
SELECT deptno, AVG(sal), MAX(sal) FROM emp GROUP by deptno HAVING AVG(sal) < 2000;
?
更多文章、技術交流、商務合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
