Student学生表(Sid,Sname)
Course科目表(Cid,Cname,Tid)
SC成绩表(Sid,Cid,Score)
Teacher表(Tid,Tname)
1)查询每科最好的前两名学生信息
2)查询两门以上不及格的同学学号及平均成绩
一直都不太能弄好三表联查,谢谢各位了!!
------解决方案--------------------
- SQL code
-- 1)查询每科最好的前两名学生信息with t as(select row_number() over(partition by Cid order by Score desc) rn,sid,Cid from SC)select c.Cname,a.rn,b.Snamefrom t ainner join Student b on a.Sid=b.Sidinner join Course c on a.Cid=c.Cidwhere a.rn<=2-- 2)查询两门以上不及格的同学学号及平均成绩select a.sid,avg(Score) 'avgScore'from Sc ainner join(select Sid from Sc where Score<60 group by Sid having count(*)>2) bon a.Sid=b.Sid