聚合函数
使用技巧,先不管聚合函数,先使用sql语句把要聚合的内容先查出来,然后再用聚合函数把要聚合的内容包起来
1. count聚合函数,用来统计行数
~ 统计一个班共有多少学生
select count(*) from exam;
使用别名name_count
select count(*) name_count from exam;
~ 统计数学成绩大于70的学生有多少?
select count(*) from exam where math>70;
~ 统计总分大于230的人数有多少?
select count(*) from exam where math+english+chinese>230;
2. sum聚合函数,求符合条件的某列的和值
~ 统计一个班级数学总成绩?
select sum(math) from exam;
~ 统计一个班级语文,英语,数学各科的总成绩
select sum(math),sum(english),sum(chinese) from exam;
~ 统计一个班级语文,英语,数学的成绩总和
select sum(math+english+chinese) from exam;
~ 统计一个班语文成绩的平均分
select sum(chinese)/count(*) from exam;
3. avg聚合函数,求符合条件的列的平均值
~ 求一个班级数学平均分
select math from exam;
~ 求一个班级总分平均分
select avg(math+english+chinese) from exam;
4. max/min聚合函数,求符合条件的列的最大值和最小值
~ 求班级最高分和最低分
select max(math+english+chinese) from exam;
select min(math+english+chinese) from exam;