--查询学生平均分数
--round保留小数2位,avg求平均分,把xuehao 列名显示为学号、把score列名显示为平均分
select xuehao 学号,round(avg(score),2) 平均分
from SC
group by xuehao --通过xuehao分组
--查询每位学生选修的课程门
select xuehao 学号,count(1) 课程门数
--count()函数中往往用数字1或者*,因为任何列都可以统计函数行数
--查询每门课程的最高分、最低分、平均分
select cid 课程号,max(score) 最高分,min(score) 最低分,avg(score) 平均分
from SC group by cid
--查询平均分大于80分学生的学号和平均分
select xuehao 学号,round(avg(score),2) 平均分
from SC
group by xuehao
having round(avg(score),2)>80
--查询男生的平均分
--从SC表里查询xuehao。查询score的平均值,小数保留两位
select xuehao 学号,round(avg(score),2)from SC
--先执行括号里面的(在shudent表里根据cid查询出男生),再查平均分
where SC.xuehao in(select xuehao from Student where gender='男')
--通过学号分组
group by xuehao
--大于75,结果保留两位小数
having round(avg(score),2)>75
--排序方向:升序asc,降序desc,asc可省略
order by round(avg(score),2) desc
--根据生日排序
select *from Student
order by grade desc,class,birthday desc
sql查询语句-平均分、最高最低分、判断、排序
最新推荐文章于 2025-03-18 00:46:24 发布