#多行函数
select max(sal),min(sal),sum(sal),count(empno),count(job),AVG(sal) from emp; #最大值,最小值,求和,计数,工作计数,求平均值
select min(hiredate),MAX(ename),min(ename) ,sum(comm),AVG(comm)from emp; #查看最老的员工,名字最大的员工
#工作的时候不要把普通列和组函数写在一起,虽然不会报错,但是结果是错误的
SELECT MAX(sal), ename from emp;
select sal,ename from emp ORDER BY sal desc limit 1;
#分组函数:按照某一个条件进行分组,每一组返回对应的结果
#GROUP BY可以对指定的列进行分组,列尽量相同
#having可以对分组之后的数据进行过滤,所以能出现在having中的比较项一定是被分组的列或者是组函数
select job,max(sal) from emp GROUP BY job; #查询工作岗位的最高薪资
select deptno,min(sal) from emp GROUP BY deptno; #查询部门的最低薪资
select deptno,AVG(sal) from emp where sal<3000 group by deptno ORDER BY deptno; #查询部门3000一下的平均薪资
select deptno,AVG(sal) from emp where sal<9000 group by deptno having avg(sal)>4500; #查询大于4500小于9000的工资的部门
#查询哪个部门每个月的津贴总数超过700
SELECT deptno,sum(comm) from emp where comm is not null GROUP BY deptno having SUM(comm)>700;
#查询部门1、2的平均薪资
select deptno,avg(sal) from emp where deptno in (1,2) group by deptno;
SELECT deptno,AVG(sal) from emp GROUP BY deptno having deptno in (1,2);
#查询每个部门名字中含有A的平均薪资
SELECT deptno,AVG(sal) from emp where ename like '%A%' GROUP BY deptno ;
#查询1、2部门中,并且在11月份入职的员工。每个部门中的平均薪资高于4500的工作是什么,并且按照部门、工作的平均值进行排序
SELECT
deptno,
job,
avg( sal )
FROM
emp
WHERE
deptno IN ( 1, 2 )
AND EXTRACT( MONTH FROM hiredate ) = 12
GROUP BY
deptno,
job
HAVING
AVG( sal ) > 4500;
数据库(多行函数)
最新推荐文章于 2022-07-21 12:09:38 发布