聚集函数是综合信息的统计函数,也称为聚合函或集函数,包括计数、求最大值、求最小值、求平均值和求和等。聚集函数可作为列标识符出现在select子句的目标列、having子句的条件中或order by子句中。
在SQL查询语句中,如果又group by子句,则语句中的函数分组统计函数,否则,语句中的函数为全部结果集的统计函数。
| 聚集函数 | 具体用法 | 含义 |
| count | count(*) | 统计元组的个数 |
| count | count([distinct|all]<列名>) | 统计一列中值的个数 |
| sum | sum([distinct|all]<列名>) | 计算一列值的总和(此列必须为数值型) |
| avg | avg([distinct|all]<列名>) | 计算一列值的平均值(此列必须为数值型) |
| max | max([distinct|all]<列名>) | 求一列中最大的值 |
| min | min([d0istinct|all]<列名>) | 求一列中最小的值 |
查询学生总数
- select count(*)
-
- form stundent;
查询选修了课程的学生人数
- select count(distinct sno)
-
- from sc;
计算c01号课程的学生平均成绩
- select avg(degree)
-
- from sc
-
- where con="c01";
查询选修了c01号课程的学生最高分和最低分
- select max(degree) as 最高分,min(degree) as 最低分
-
- from sc
-
- where con="c01";
查询学号为“2012010112”的学生的总成绩及平均成绩
- select sum(degree) as 总成绩,avg(degree) as 平均成绩
-
- from sc
-
- where sno="2012010112";
查询有考试成绩的学生人数
- select count(distinct sno)
-
- from sc
-
- where degree is not null;