組函數也叫聚合函數,用來對一組值進行運算,並且可以返回單個值
常見的組函數:
(1)count(*),count(列名) 統計行數:找到所有不為 null 的數據來統計行數
(2)avg(列名) 平均數
(3)sum(列名) 求和
(4)max(列名) 求最大值
(5)min(列名) 求最小值
scott是oracle中的一個示范用戶,主要用於測試。它自帶一些測試用的數據方便我們測試,由於是oracle中的一個對象,因此scott.emp表示數據庫中的員工表。
-- 首先,查看emp 表中的所有數據。
select * from scott.emp;
--(1)統計 emp 表中的總行數
select count(*) from scott.emp;
--統計 emp 表中屬性列為ename的總行數。
select count(ename) from scott.emp;
--統計 emp 表中屬性列為comm的總行數,由於有10項為空,所以行數為4
select count(comm) from scott.emp;
--(2)平均值
-- 統計 emp 表中屬性列為comm的平均獎金。
-- 一般做法,平均獎金 = 總獎金/總員工數。
select sum(comm) / count(comm) from scott.emp;
-- 使用 avg() 求均值
select avg(sal) from scott.emp;
--(3)使用sum() 求和
select sum(sal) from scott.emp;
--(4)最大值
select max(sal) from scott.emp;
--(5)最小值
select min(sal) from scott.emp;