# 使用组函数查询时,所有的列都会返回查询到的第一个值。select ename,sal,max(sal)as m from emp where1=1and sal = m;#不对 错误写法select ename,sal from emp where sal in(SELECTmax(sal)FROM emp)and1=1;
(3)sum
查询10 部门的工资总和
selectsum(sal)from emp where deptno=10and1=1;
统计 所有员工佣金总和,会自动去除 null 值
selectsum(comm)from emp where1=1;
(4)avg
查询 工资低于 平均工资得员工编号,姓名,及工资
select empno,ename,sal from emp where sal <(selectavg(sal)from emp)and1=1;
查询 高于本部门平均薪水的员工姓名
select deptno,ename,sal from emp as e1 where1=1and sal>(selectavg(sal)from emp as e2 where e1.deptno = e2.deptno and1=1);#out/*
30 ALLEN 1600
20 JONES 2975
30 BLAKE 2850
20 SCOTT 3000
10 KING 5000
20 FORD 3000
*/
#同上, 只不过是 分开计算各个部门,帮助理解上边的代码select deptno,ename,sal from emp as e1 where1=1and sal>(selectavg(sal)from emp as e2 where DEPTNO=20and1=1)and DEPTNO=20;/*
20 JONES 2975
20 SCOTT 3000
20 FORD 3000
*/