You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Source : https://leetcode.com/problems/highest-grade-for-each-student/
Author : liuyubobobo
Time : 2022-04-26
Using JOIN
SELECTEnrollments.student_id, MIN(Enrollments.course_id) AS course_id, t.max_gradeAS grade
FROM
Enrollments JOIN
(
SELECT student_id, MAX(grade) AS max_grade
FROM Enrollments
GROUP BY student_id
) t
ONEnrollments.student_id=t.student_idANDEnrollments.grade=t.max_gradeGROUP BYEnrollments.student_idORDER BY student_id ASC
Using Nested Query, check two field in the query
SELECT student_id, MIN(course_id) AS course_id, grade
FROM Enrollments
WHERE (student_id, grade) IN
(
SELECT student_id, MAX(grade) AS grade
FROM Enrollments
GROUP BY student_id
)
GROUP BY student_id
ORDER BY student_id ASC