注:附件源代码在资源处可下载(demo8.sql)
use stusys;
#1.聚合函数:求学生的总人数
select count(*) as 总人数
from student;
执行结果:
#2.查询通信专业学生的总人数
select count(*) as 总人数
from student
where speciality='通信';
执行结果:
#3.sum()函数用于求一组数据的总和:查询1201的课程的总分
select sum(grade) as 课程1201总分
from score
where cno='1201';
执行结果:
#4.avg()函数用于求出一组数据的平均值,
#max()函数用于求出一组数据的最大值,
#min()函数用于求出一组数据的最小值。
#查询8001课程的最高分、最低分、平均成绩
select max(grade) as 课程8001最高分, min(grade) as 课程8001最低分, avg(grade) as 课程8001平均成绩
from score
where cno='8001';
执行结果:
#5.group by子句用于指定需要分组的列:查询各门课程的最高分、最低分、平均成绩
select cno as 课程号, max(grade) as 最高分, min(grade) as 最低分, avg(grade) as 平均成绩
from score
where not grade is null
group by cno;
执行结果:
#6.having子句用于对分组按指定条件进一步进行筛选,过滤出满足指定条件的分组:查询平均成绩在90分以上的学生的学号和平均成绩
select cno as 课程号, avg(grade) as 平均分数
from score
where cno like '8%'
group by cno
having count(*)>5;
执行结果: