数据库的分组查询和统计查询
在group by之后不能使用where,只能使用having,在group by之前可以使用where,即表示对过滤后的结果分组
select sname,sum(score) from s_k group by sname
select count(distinct sname) from s_k
select sname,avg(score) from s_k group by sname
select kname,avg(score),max(score),min(score) from s_k group by kname select avg(score),max(score),min(score) from s_k where kname='语文' select sname,sum(score),avg(score) from s_k group by sname
select * from s_k;
select kname,avg(score) avgs from s_k group by kname
一. 使用聚集函数:
1. 查询学生总人数:
Select Count(*) as 学生总数 from student
2. 查询选修了课程的学生总数:
select count(distinct sno) as 选课学生总数 from sc
3. 查询所有课程的总学分数和平均学分数,以及最高学分和最低学分: select sum(credit) as 总credit,avg(credit) as 课程平均学分,max(credit) as 最高学分, min(credit) as 最低学分 from course
4. 计算1号课程的学生的平均成绩, 最高分和最低分: