接着昨天的继续
18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT c.C AS '课程id',c.Cname AS '课程名称',MAX(s.score) AS '最高分',MIN(s.score) AS '最低分' ,
AVG(s.score) AS '平均分' ,(SELECT COUNT(1) FROM sc WHERE sc.C=s.C AND sc.score>=60)/
(SELECT COUNT(1) FROM sc WHERE sc.C=s.c)及格率
FROM sc s,course c WHERE s.C=c.C GROUP BY c.C
19、按各科成绩进行排序,并显示排名
SELECT c.*,(SELECT COUNT(1) FROM sc c1 WHERE c1.c='01' AND c1.score>c.`score`)+1
mc FROM sc c WHERE c.c='01' ORDER BY mc
20、查询学生的总成绩并进行排名
-- 未排名版
SELECT s.S,s.Sname,SUM(c.score)
,(SELECT COUNT(1) FROM sc c1 WHERE c1.score=c.score AND c1.C
21、查询不同老师所教不同课程平均分从高到低显示
SELECT t.T,t.Tname,c.C,c.Cname,AVG(s.score) FROM sc s,course c,teacher t WHERE s.C=c.C AND c.T=t.T
GROUP BY t.T,c.C ORDER BY AVG(s.score) DESC
22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
SELECT s.S,s.Sname,a.c,a.score,a.mc FROM student s,(SELECT c.S,c.C,c.score,
(SELECT COUNT(*) FROM sc cc WHERE cc.c='01' AND cc.score>c.score)+1
mc FROM sc c WHERE c.C='01' HAVING mc BETWEEN 2 AND 3
UNION ALL SELECT c.S,c.C,c.score,
(SELECT COUNT(*) FROM sc cc WHERE cc.c='02' AND cc.score>c.score)+1 mc
FROM sc c WHERE c.C='02' HAVING mc BETWEEN 2 AND 3
UNION ALL SELECT c.S,c.C,c.score,
(SELECT COUNT(*) FROM sc cc WHERE cc.c='03' AND cc.score>c.score)+1
mc FROM sc c WHERE c.C='03' HAVING mc BETWEEN 2 AND 3
) a WHERE s.S=a.s
23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比
SELECT c.c,c.Cname,d.rs '85-100',d.bfb,e.rs '70-85',e.bfb FROM(
SELECT * FROM course ) c LEFT JOIN (SELECT a.c,a.cc rs,a.cc/b.cc bfb FROM (
SELECT c.c,COUNT(*) cc FROM sc c WHERE c.score>=85 GROUP BY c.c) a,
(SELECT c,COUNT(*) cc FROM sc GROUP BY sc.C) b
WHERE a.c=b.c ) d ON c.c=d.c LEFT JOIN
(SELECT a.c,a.cc rs,a.cc/b.cc bfb FROM (
SELECT c.c,COUNT(*) cc FROM sc c WHERE c.score>=70 AND c.score<85 GROUP BY c.C) a,
(SELECT c,COUNT(*) cc FROM sc GROUP BY sc.C) b
WHERE a.c=b.c ) e ON c.c=e.c
24、查询学生平均成绩及其名次
SELECT a.S,
@i:=@i+1 AS '不保留空缺排名',
@k:=(CASE WHEN @avg_score=a.avg_s THEN @k ELSE @i END) AS '保留空缺排名',
@avg_score:=avg_s AS '平均分'
FROM (SELECT S,ROUND(AVG(score),2) AS avg_s FROM sc GROUP BY S)a,(SELECT @avg_score:=0
,@i:=0,@k:=0)b;
25、查询各科成绩前三名的记录
SELECT a.S,a.C,a.score FROM sc a
LEFT JOIN sc b ON a.C = b.C AND a.score