where + group by
group by 字句 和 where条件语句结合在一起使用,当结合在一起时,where在前,group by 在后。即先对select xx from xx的记录集合用where进行筛选,
然后再使用group by 对筛选后的结果进行分组 使用having字句对分组后的结果进行筛选。
区别
having和where的用法区别:
having只能用在group by之后,对分组后的结果进行筛选(即使用having的前提条件是分组)。
where肯定在group by 之前。
where后的条件表达式里不允许使用聚合函数,而having可以。
where + group by + having +order by
1:使用where 对条件进行筛选
2: group by 分组筛选
3:having 分组后在筛选
4:order by 排序 desc 递减 asc 升序
select name, avg(score)
from t_student
where score>=60 and course_id != '001'
group by name
having avg(score) >=80
order by avg(score) desc
对sql进行拆分:
查询 t_student表的name,(姓名),平均分(score)
要求 查询的学生的分数必须大于60,且课程编号不能为001
根据学生姓名进行分组查找
对查找的结果筛选出平均分数大于80分的学生姓名,平均分 安装递减顺序输出