问题描述
编写查询以显示在每个学科中获得最高分的学生的姓名,并按学科名称以升序排列。如果有多个顶部,请按字母顺序显示其名称。 将其显示为subject_name和student_name。 O / P:第一列-subject_name 第二列-学生名
我的答案-
select su.subject_name,st.student_name from subject su,student st,mark m
where m.student_id = st.student_id and m.subject_id = su.subject_id
and m.value = (select max(value) from mark group by subject_id);
错误-
and m.value = (select max(value) from mark group by subject_id)
*
ERROR at line 3:
ORA-01427: single-row subquery returns more than one row
我知道的是,我将不得不进行另一个子查询,例如
and m.value =(select..... (select max(value) from mark group by subject_id));
但是我不明白。
解决方法
没关系,我们将不得不使用IN运算符和俱乐部值,subject_id,因为它们是一组。
SELECT su.subject_name,st.student_name
FROM student s
JOIN mark m ON st.student_id = m.student_id
JOIN subject su ON m.subject_id = su.subject_id
WHERE (m.value,su.subject_id) IN (
SELECT max(value),subject_id FROM mark GROUP BY subject_id
)
ORDER BY su.subject_name,st.student_name;