目录
分组函数
简单使用
支持的参数类型
sum,avg
max,min
count
是否忽略null
sum,avg
min,max
count
案例
分组查询
引入
添加筛选条件
添加分组后的筛选(having)
按多个字段分组
添加排序
案例
功能:做统计使用,又称为聚合函数,统计函数或组函数。
分类:
sum 求和
avg 平均值
max 最大值
min 最小值
count 计算个数
特点:1.sum,avg一般用于处理数值型。max,min,count可以用于处理任何类型。
2.以上分组函数都忽略null值
3.可以和distinct搭配实现去重运算
5.和分组函数一同查询的字段要求是group by 后的字段
#简单使用
SELECT SUM(salary)FROM employees;
SELECT AVG(salary)FROM employees;
SELECT MIN(salary)FROM employees;
SELECT MAX(salary)FROM employees;
SELECT COUNT(salary)FROM employees;
SELECT SUM(salary) 和,ROUND(AVG(salary),2) 平均值,
MIN(salary) 最小值,MAX(salary) 最大值,COUNT(salary) 数量
FROM employees;
#支持的参数类型
SELECT SUM(last_name),AVG(last_name)
FROM employees;#结果为0,0
SELECT SUM(hiredate),AVG(hiredate)
FROM employees;#结果不为0
SELECT MAX(last_name),MIN(last_name)
FROM employees;#结果为姓名
SELECT MAX(hiredate),MIN(hiredate)
FROM employees;#结果为日期
SELECT COUNT(last_name)
FROM employees;#107
SELECT COUNT(commission_pct)
FROM employees;#35
#是否忽略null
SELECT SUM(commission_pct),AVG(commission_pct),
SUM(commission_pct)/35,SUM(commission_pct)/107
FROM employees;
#sum忽略null
SELECT MAX(commission_pct),MIN(commission_pct)
FROM employees;
#max,min忽略null
SELECT COUNT(commission_pct)
FROM employees;
#count忽略null
#查询员工表中最大入职时间和最小入职时间的相差天数
SELECT MAX(hiredate) 最大入职时间,
MIN(hiredate) 最小入职时间,
DATEDIFF(MAX(hiredate),MIN(hiredate)) 相差天数
FROM employees;
#查询部门编号为90的员工个数
SELECT COUNT(*)
FROM employees
WHERE department_id=90;
语法:
select 分组函数,列(要求出现在group by的后面)
from 表
where 筛选条件
group by 分组的列表
order by 子句
注意:
查询列表必须特殊,要求是分组函数和group by后出现的字段
特点:
1.分组函数条件是放在having子句中
2.能用分组前筛选的,优先考虑分组前筛选
#每个部门的平均工资
SELECT AVG(salary) 平均工资,`department_id`
FROM employees
GROUP BY department_id;
#查询每个工种的最高工资
SELECT MAX(salary),job_id
FROM employees
GROUP BY job_id;
#查询每个位置上的部门个数
SELECT COUNT(*),location_id
FROM departments
GROUP BY location_id;
#添加筛选条件
#查询邮箱中包含a字符的,每个部门的平均工资
SELECT AVG(salary) 平均工资,department_id
FROM employees
WHERE email LIKE'%a%'
GROUP BY department_id;
#查询有奖金的每个领导手下的员工的最高工资
SELECT MAX(salary) 最高工资,manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;
注意:having后不能接别名,
where后不能加别名
别名不能用于判断
#添加分组后的筛选(having)
#查询哪个部门的员工个数大于2
SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>2;
#查询每个工种有奖金的,最高工资>12000的员工的最高工资和工种编号
SELECT MAX(salary) 最高工资,job_id 工种编号
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY 工种编号
HAVING MAX(salary)>12000;
#查询领导编号>102的每个领导手下,
#最低工资>5000的员工的
#领导编号和最低工资
SELECT MIN(salary) 最低工资,manager_id 领导编号
FROM employees
WHERE manager_id>102
GROUP BY 领导编号
HAVING MIN(salary);
#案例:按员工姓名长度分组,
#查询每一组的员工个数,
#筛选员工个数>5的有哪些
SELECT COUNT(*),LENGTH(last_name)
FROM employees
GROUP BY LENGTH(last_name)
HAVING COUNT(*)>5;
#按多个字段分组
#案例:查询每个部门每个工种的员工平均工资
SELECT AVG(salary),department_id,job_id
FROM employees
GROUP BY department_id,job_id;
#添加排序
#案例:查询每个部门每个工种的员工平均工资,
#并且按平均工种的高低显示
SELECT AVG(salary),department_id,job_id
FROM employees
GROUP BY job_id,department_id
ORDER BY AVG(salary) DESC;
#查询各工种员工工资的最大值,最小值,平均值,总和
#并按工种号升序
SELECT MAX(salary),MIN(salary),AVG(salary),SUM(salary),job_id
FROM employees
GROUP BY job_id
ORDER BY job_id ASC;
#查询最高工资和最低工资的差距
SELECT MAX(salary),MIN(salary),MAX(salary)-MIN(salary) 差距
FROM employees;
#查询各个管理员手下员工的最低工资,
#最低工资不能低于6000,
#没有管理员的不计算在内
SELECT MIN(salary),manager_id
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING MIN(salary)>=6000;
#查询所有部门的编号,员工数量,工资平均值
#按工资平均值降序
SELECT department_id,COUNT(*),AVG(salary)
FROM employees
GROUP BY department_id
ORDER BY AVG(salary) DESC;
#具有各个job_id的员工个数
SELECT COUNT(*),job_id
FROM employees
GROUP BY job_id;