查询--聚集查询

聚合函数

计数:Count(可加 distinct 去重  列名)

   select count(*) from Student //包括null值

   select count(sno) from Student//自动过滤null值

求和:Sum(可加 distinct 去重 列名)自动过滤null值

平均值:Avg(可加 distinct 去重 列名)自动过滤null值

最大/最小:Max Min(可加 distinct 去重  列名)自动过滤null值

分组:group by 列名 表示按。。分组,只有分组属性才能和聚合属性同时出现在 select 后边

select Cno,Count(*) --先分组,按Cno分组,Count(*)计算每个组的行数
from SC
group by Cno --如果没有按照Cno分组,则Cno不能出现在Select后边

排序:order by  同样也是 

--查找各个学院姓张的学生,输出其人数,降序排序
select Sdepe,count(*) 
from Student where Sname like  '张%'
group by Sdepe
order by count(*) desc
--查询各个学院各种性别各有多少人,
--查询结果先按照所在系降序,在按照性别降序排列
select  Sdepe,Ssex,count(*) '人数'
from Student 
group by Sdepe,Ssex
order by Sdepe desc,Ssex Asc

having:对分组过后的结果进行筛选 要求同上

功能等同于where 但where不能出现在聚合函数

--查询选修课程人数大于3人的课程
select Cno,Count(*)'课程人数'
from SC
group by Cno
having Count(*) >=3
--查询有3门以上选修课程 且课程分达到85分以上的学生学号和达到85分以上的课程数
--按照课程数降序
Select  Sno,count(distinct Cno)
from SC where grade >=80
group by Sno
having count(distinct Cno)>=3 
order by count(distinct Cno) desc

 

posted @ 2022-04-11 15:49  山上的树  阅读(92)  评论(0)    收藏  举报