查询各科成绩前三名的记录
但凡遇到排名类问题,一个基本思路是直接自联结,然后根据要求,在where中添加条件即可;本题要求科目前三,则筛选同科目比自己大成绩数小于3即可
select s.cid,s.sid,s.score
from sc s
where (select count(*)
from sc a
where s.cid = a.cid
and s.score < a.score) < 3
order by cid asc, s.score desc;