SQL基础-having
程序员文章站
2022-07-13 16:12:17
...
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数据库笔记”公众号,一起学习吧!