#按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select * from score
left join (
select sid,avg(score) as avgScore from score
group by sid
)r
on score.sid = r.sid
order by avgScore desc;
#查询各科成绩最高分,最低分和平均分
#显示形式:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
#及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
#要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select score.cid,
max(score.score) as 最高分,
min(score.score) as 最低分,
AVG(score.score) as 平均分,
count(*) as 选修人数,
sum(case when score.score>=60 then 1 else 0 end )/count(*) as 及格率,
sum(case when score.score>=70 and score.score<80 then 1 else 0 end )/count(*) as 中等率,
sum(case when score.score>=80 and score.score<90 then 1 else 0 end )/count(*) as 优良率,
sum(case when score.score>=90 then 1 else 0 end )/count(*) as 优秀率
from score
GROUP BY score.cid
ORDER BY count(*) DESC , score.cid ASC;
#查询学生总成绩,并进行排名,总分重复不保留名次空缺
#SQL中的变量
set @crank=0;
#查询语句
select q.sid, total, @crank := (@crank +1) as 名次 from(
select score.sid, sum(score.score) as total from score
group by score.sid
order by total desc)q;
#查询学生信息,所选课程信息,所选课程老师信息,所选课程成绩信息
select * from student s,course c,teacher t,score sc where
s.sid = sc.sid and sc.cid = c.cid and c.tid = t.tid;