Skip to content

1112. Highest Grade For Each Student 👍

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
WITH
  RankedEnrollments AS (
    SELECT
      student_id,
      course_id,
      grade,
      RANK() OVER(
        PARTITION BY student_id
        ORDER BY grade DESC, course_id
      ) AS `rank`
    FROM Enrollments
  )
SELECT
  student_id,
  course_id,
  grade
FROM RankedEnrollments
WHERE `rank` = 1;