SELECT [column,] group_function(column)
FROM TABLE
WHERE [condition]
[GROUP BY column]
[HAVING group_function(column) expression]
[ORDER BY column | group_function(column) expression]
注意点1
我们如果需要对group by 的东西进行限制的话,需要使用where对其行的过滤
使用having对分组的数据进行过滤
注意点2
我们可以取别名
select count(*) c,length(last_name) name_length
from employees
group by name_length
having count(*)>5;
多个字段进行分组
多个字段分组,用,分割,并且没有先后顺序
select avg(salary),department_id,job_id from employees group by department_id,job_id;
# 计算每个工种,每个部门的平均工资
使用order by 对分组函数数据进行排序
select avg(salary),department_id from employees group by department_id order by avg(salary) desc