文章目录
简单分组
use teaching
select ssex count(ssex) 人数
from student
group by ssex
use teaching
select sno,count(sno) 选修课程门数 from sc
group by sno
having count(cno)>=2
where与having的区别:
1.where 是对整个表进行筛选。having 是对groupby以列分组查询之后的组加条件,筛选出满足条件的组。
2.having 一般使用聚合函数,而where不能使用
cube和rollup
use teaching
select cno ,avg(score) as ‘平均成绩’,count(sno) as ‘选修人数’
from sc
group by cno
with cube
use teaching
select specialty ,ssex ,count(*) as ‘人数’ from student
group by specialty,ssex
with rollup
数据汇总
use teaching
select sno,sname,ssex from student where specialty=‘计算机’
comput count(sno)