查询--聚集查询

聚合函数

计数: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 @   山上的树  阅读(75)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
点击右上角即可分享
微信分享提示