SQLserver 聚合,分组,having

本文介绍了多种SQL查询语句的使用,包括从数据表中筛选特定信息、使用LIKE操作符进行模糊匹配、聚合函数的应用以及分组查询。通过实例展示了如何查询课程信息、学生成绩、统计课程数量和学生总分,并利用HAVING子句过滤结果。这些技巧对于理解和操作数据库至关重要。
摘要由CSDN通过智能技术生成
use srs

select * from t 

where tn like '李%'
--从数据表 C 中查询所有课程名中含有“原理”两个字的课程全名。
select cn from c

where cn like '%原理%'

--从数据表C中查询课程编号以“C”开头或者以“P”开头的所有课程信息
select * from c

where cno like 'C%' or  cno like 'p%'


select * from c

where cno like '[cp]%'

--从数据表C中查询课程编号以“C”到“D”开头的所有课程信息

 
select * from c

where cno like  '[c-d]%'

--从数据表C中查询课程编号不是以“C”开头或者以“D”开头的所有课程信息

select * from c

where cno like '[^c-d]%'

select * from c

where cno not like '[c-d]%'


--有效成绩
select * from sc

where grade is not null

--聚合函数
select count(Grade) as 有效成绩,max(Grade) as 最高分, min(Grade) as 最低分 from sc
where cno='C0201'



--分组查询
select   cno ,COUNT(sno) as num
from sc
group by cno


--1.查询T表中各系的教师人数。
select dept, COUNT(*) as num
from t
group by dept 
--2.在SC表中统计每位学生的总分
select sno ,SUM(Grade) as  sum
from sc
group by sno

--3、在S表中分别统计各系别男女生的人数
select  dept ,sex,COUNT(*) as num
from s
group by dept ,sex

--4,查询SC表C0201课程的最低分(显示课程号、最低分)
select  cno ,MIN(grade) as min
from sc 
where cno='c0201'
group by CNo

--having

select cno,AVG(grade) 平均分
from sc
group by cno
having AVG(grade)>80


select sno,sum(grade) 总分
from sc
group by sno
having sum(grade)>150


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值