分组函数(聚合函数)
sum(列名) --求和
AVG(列名) --求平均
MIN(列名) --求最小
MAX(列名) --求最大
COUNT(列名 | * |数字) --计数
group by
根据要求,按照规则分组,统一分析各组的情况,每一组返回一个值。
SELECT *|列名|常量|计算|函数
FROM 表名
WHERE 过滤条件
GROUP BY 分组内容
HAVING 过滤条件
ORDER BY 排序内容 ASC|DESC;
1.统计除10号部门的其他部门中,哪些部门的最高薪资达到三千
2.查询平均薪资超过1800的部门,显示部门编号及平均薪资
3.查询薪资合计超过5000的岗位及其平均薪资
4.查询部门人数超过4人的部门编号及薪资合计
5.查询各部门下,人数超过两人的岗位有哪些,显示部门编号、岗位名称
6.查询姓名重复的员工的姓名
SELECT deptno
FROM emp
WHERE deptno !=10
GROUP BY deptno
HAVING MAX(sal)>=3000;
SELECT deptno,AVG(sal)
FROM emp
GROUP BY deptno
HAVING AVG(sal)>1800;
SELECT job,AVG(sal)
FROM emp
GROUP BY job
HAVING SUM(sal)>5000;
SELECT deptno,SUM(sal)
FROM emp
GROUP BY deptno
HAVING COUNT(deptno)>4;
SELECT deptno,job
FROM emp
GROUP BY deptno,job
HAVING COUNT(job)>2;
SELECT ename
FROM emp
GROUP BY ename
HAVING COUNT(ename)>=2;
-- 1. 查找每位客户的订单数量和总金额
GROUP BY 根据客户进行分组
select customer_id,count(order_id) as '订单数量',SUM(total_amount) FROM orders GROUP BY customer_id
-- 2. 查找订单总金额超过 50.00 的客户
-- 2. 查找订单总金额超过 50.00 的客户
group by 根据客户进行分组 having 查找后的条件
select customer_id,SUM(total_amount) FROM orders group by customer_id having SUM(total_amount)>50