函数:oracle服务器先事写好的一段具有一定功能的程序片段,内置于oracle服务器,供用户调用。
单行函数:输入一个参数,输出一个结果。
多行函数:输入多个参数,或者是内部扫描多次,输出一个结果。
多行函数:count/sum/avg/max/min
--统计公司有多少个不重复的部门
select count(distinct deptno) from emp;
--统计有佣金的员工人数
select count(comm) from emp;
注意:今天讲的这些多个行函数,不统计NULL值
--按部门求出该部门平均工资,且平均工资取整数,采用截断
select deptno "部门编号",trunc(avg(sal),0) "部门平均工资" from emp group by deptno;
--查询部门平均工资大于2000元的部门
select deptno "部门编号",trunc(avg(sal),0) "部门平均工资" from emp group by deptno having trunc(avg(sal),0) > 2000;
--按部门平均工资降序排列
select deptno "部门编号",trunc(avg(sal),0) "部门平均工资" from emp group by deptno having trunc(avg(sal),0) > 2000 order by 2 desc;
group by子句的细节:
1)在select子句中出现的非多行函数的所有列,【必须】出现在group by子句中
2)在group by子句中出现的所有列,【可出现可不现】在select子句中