查询每门课程最高分的学生的学号,课程号,成绩

成绩表score的结构:

如何查询每门课程最高分的学生的学号,课程号,成绩?

答案:

select t1.sid,t1.cid,t1.score
from score t1
where t1.score = 
(
    select max(t2.score)
    from score t2
    where t2.cid = t1.cid
    group by t2.cid
)

如果要知道学生的姓名:

select t1.sid,s.name,t1.cid,t1.score
from score t1
inner join stu s
on t1.sid=s.id
where t1.score = 
(
    select max(t2.score)
    from score t2
    where t2.cid = t1.cid
    group by t2.cid
)

 参考:http://zhidao.baidu.com/link?url=1hVfu2fzddmSlZkn0El-bpyuQHX2egJGRsTLqDs8-UQOwjO5XxlBO_PEZ6yI9Vvpf9oMe-6BXV98W-SIT0ynsiDmhZbX95rL5P49hiPDLe_

原文地址:https://www.cnblogs.com/aaronhoo/p/5712209.html