组函数也叫聚合函数,用来对一组值进行运算,并且可以返回单个值
常见的组函数:
(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;