主要是子查询内容与父查询内容相关时,的一个思路过程 参考解析:[sql查询各科成绩前三名----详述过程,思路清晰不烧脑](https://blog.youkuaiyun.com/and52696686/article/details/107591245)
具体有三种方法可以实现:
select * from(select *,rank()over (partition by C# order by score desc)A from SC)B where B.A<=3
--18. 查询各科成绩前三名的记录(方法 1)
select a.S#,a.C#,a.score from SC a
left join SC b on a.C#=b.C# and a.score<b.score
group by a.S#,a.C#,a.score
having COUNT(b.S#)<3
order by a.C#,a.score desc
--18. 查询各科成绩前三名的记录(取 a 的最高分与本表比较)(方法 2)
select * from SC a where (select COUNT(*)from SC where C#=a.C# and score>a.score)<3
order by a.C#,a.score desc
--18. 查询各科成绩前三名的记录(取 a)(方法 3)