如何获取在每个学科中获得最高学位的学生的姓名,以及如果有多个拔头奖,则按升序排列他们的姓名

时间:2019-06-20 05:37:43

标签: join subquery rdbms

Image about the tables and their links

现在我想先获得学科名称,然后再获得最高分的学生姓名。这里的mark是标记表中的值,student_id和subject_id就像主键一样,在标记表中是外键

select s2.subject_name,s1.student_name
from student s1
inner join 
mark m
on 
s1.student_id=m.student_id
inner join 
subject s2
on 
s2.subject_id=m.subject_id
where m.value in
(select max(value) from mark group by subject_id)
 order by s1.student_name;
Actual output

     SUBJECT_NAME                  STUDENT_NAME
------------------------------ ------------------------------
Computer Organization          Anitha
Computer Organization          Kokila
Computer Organization          Mithali
Computer Programming           Chandru
RDBMS Concepts                 Anitha
Software Engineering           Anandhi
Software Engineering           Kokila
Theory of Computation          Anitha

Expected output

     SUBJECT_NAME                     STUDENT_NAME
------------------------------ ------------------------------
Computer Organization          Anitha
Computer Organization          Mithali
Computer Programming           Chandru
RDBMS Concepts                 Anitha
Software Engineering           Kokila
Theory of Computation          Anitha

请检查我要去哪里的问题

-谢谢

0 个答案:

没有答案