50个SQL语句(MySQL版) 问题二十五

--------------------------表结构--------------------------

student(StuId,StuName,StuAge,StuSex) 学生表

teacher(TId,Tname) 教师表

course(CId,Cname,C_TId) 课程表

sc(SId,S_CId,Score) 成绩表

----------------------------------------------------------

问题二十五:查询各科成绩前三名的记录(不考虑成绩并列情况)

SELECT * FROM
(SELECT s1.S_CId,s1.Score,COUNT(DISTINCT s2.Score) Rank
FROM sc AS s1
INNER JOIN sc AS s2
ON s1.S_CId=s2.S_CId AND s1.Score<s2.Score
GROUP BY s1.S_CId,s1.Score) score
WHERE score.Rank>=1 AND score.Rank<=3 
GROUP BY score.S_CId,score.Rank;

答案仅供参考,不一定完全正确,若发现错误或有更好的,欢迎评论,互相交流,一起成长!!!

posted @ 2020-06-04 17:43  你的名字_子集  阅读(281)  评论(0编辑  收藏  举报