having语句通常与group by语句联合使用,用来过滤由分组后返回的记录集。
注意:where语句在数据分组前进行过滤
语法:
select column_name, aggregate_function(column_name)
from table_name
where column_name operator value
group by column_name
having aggregate_function(column_name) operator value
示例:
表的定义如下:
create table [dbo].[StuScores](
[StuId] [int] not null,
[CusId] [int] not null,
[Score] [float] not null,
[TestDate] [datetime] null
)
1.普通查询如下:
select * from [dbo].[StuScores]

2.用having查询各课程最高分数大于90分的:
select [CusId],max([Score])
from [dbo].[StuScores]
group by [CusId]
having max([Score])>90

3.查询学生总分大于100的学生编号及总成绩
select [StuId],sum([Score]) as 总成绩
from[dbo].[StuScores]
group by[StuId]
having sum([Score])>100
end
有兴趣的小伙伴可以关注“SQL数据库笔记”公众号,一起学习吧!
最后修改时间:2019-12-16 13:22:23
文章转载自SQL数据库笔记,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




