展开全部
请参考下列SQL语句:select student.sno,student.sname,student.sdept,
course.cname,t.maxgrade from
student,course,
(select s.sno,c.sdept,s.cno,c.maxgrade
from sc s,student st,
(select a.sdept,b.cno,max(b.grade) as maxgrade
from student a,sc b where a.sno=b.sno
group by a.sdept,b.cno) c
where s.sno=st.sno and st.sdept=c.sdept and
s.grade=c.maxgrade) t where student.sno=t.sno
and course.cno=t.cno order by course.cname,student.sdept;
上述语句已经测32313133353236313431303231363533e4b893e5b19e31333365646330试通过。代码思路是:
学生表与成绩表基于学号进行连接获取每个学号所在系名,然后用院系和课程号对成绩表分组汇总,求得每个院系、每个课程的最高得分(结果集c,含系名、课程号和最高分)。然后用结果集C再次与成绩表、学生表进行比对,筛选出获得每个系、每个课程的最高分的学号并包含课程号和系名(结果集t)。最后t通过连接获取学生表中的学生姓名、课程表中的课程名完成最后输出。