聚合函數(shù)
-- 查詢 emp 表中員工的最高工資、最低工資、平均工資及工資總和
select max(sal) 最高工資,min(sal) 最低工資,avg(sal) 平均工資,sum(sal) 工資總和 from emp;
-- 返回 emp 表的員工總數(shù)
select count(x) 員工總數(shù) from emp;- 本質(zhì)也是統(tǒng)計全表的記錄數(shù),與 * 效果一致
select count(1) 員工總數(shù) from emp;
-- 返回 emp 表的部門總數(shù)
Select count(distinct deptno) 部門總數(shù)from emp;
-- 統(tǒng)計有獎金的員工人數(shù)
-- 忽略了 null,但是計算了 0
select count(comm) from emp;
-- 從業(yè)務(wù)邏輯上來講,獎金金額為 0 的員工不應(yīng)該視為有獎金的員工
select count(comm) from emp where comm!=0;
-- 查詢公司內(nèi)部不同獎金檔位的獲得人數(shù)
select comm,count(*) from emp group by comm;
-- 查詢各部門不同職位的平均工資
select deptno,job,avg(sal) as 平均工資from emp group by deptno,job;
-- 查詢各部門 clerk 的平均工資-
-- 用 having 子句篩選
select deptno,job,avg(sal) 平均工資 from emp group by deptno,job having job='clerk';
-- 用 where 子句篩選
select deptno,job,avg(sal) 平均工資 from emp where job= clerk' group by deptno,job;
-- 查詢平均工資大于 2000 的部門
select deptno,avg(sal) 平均工資from emp group by deptno
having avg(sal)>2000;








暫無數(shù)據(jù)