Highest Grade For Each Student - Problem

Imagine you're a database administrator for a university system that needs to generate academic performance reports. You have an Enrollments table that tracks every course a student has taken along with their grades.

Your task is to find the highest grade each student achieved across all their courses. However, there's a twist: if a student has the same highest grade in multiple courses, you should prioritize the course with the smallest course_id.

Database Schema:

Table: Enrollments
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| student_id    | int     |
| course_id     | int     |
| grade         | int     |
+---------------+---------+

Notes:

  • The combination of (student_id, course_id) is the primary key
  • grade is never NULL
  • Results should be ordered by student_id in ascending order

Goal: Write a SQL query that returns each student's highest grade along with the corresponding course. In case of ties, select the course with the smaller course_id.

Input & Output

basic_example.sql โ€” Basic Case
$ Input: Enrollments table: | student_id | course_id | grade | |------------|-----------|-------| | 1 | 10 | 85 | | 1 | 11 | 95 | | 1 | 12 | 88 | | 2 | 13 | 80 | | 2 | 14 | 80 |
โ€บ Output: | student_id | course_id | grade | |------------|-----------|-------| | 1 | 11 | 95 | | 2 | 13 | 80 |
๐Ÿ’ก Note: Student 1's highest grade is 95 in course 11. Student 2 has a tie with grade 80, so we pick the smaller course_id (13).
tie_breaking.sql โ€” Tie Breaking Example
$ Input: Enrollments table: | student_id | course_id | grade | |------------|-----------|-------| | 1 | 20 | 90 | | 1 | 10 | 90 | | 1 | 30 | 85 | | 2 | 15 | 92 |
โ€บ Output: | student_id | course_id | grade | |------------|-----------|-------| | 1 | 10 | 90 | | 2 | 15 | 92 |
๐Ÿ’ก Note: Student 1 has two courses with grade 90. We select course 10 (smaller course_id). Student 2 has one clear maximum.
single_course.sql โ€” Edge Case
$ Input: Enrollments table: | student_id | course_id | grade | |------------|-----------|-------| | 1 | 100 | 75 | | 2 | 200 | 88 | | 3 | 300 | 92 |
โ€บ Output: | student_id | course_id | grade | |------------|-----------|-------| | 1 | 100 | 75 | | 2 | 200 | 88 | | 3 | 300 | 92 |
๐Ÿ’ก Note: Each student has only one course, so each student's single grade is their highest grade.

Constraints

  • 1 โ‰ค student_id, course_id โ‰ค 104
  • 0 โ‰ค grade โ‰ค 100
  • Each (student_id, course_id) combination appears at most once
  • grade is never NULL

Visualization

Tap to expand
๐ŸŽ“ Student Achievement Award Generator๐Ÿ“š Raw DataS1: C10โ†’85, C11โ†’95S2: C20โ†’80, C21โ†’80S3: C30โ†’92All enrollments๐Ÿ‘ฅ Group by StudentStudent 1 partitionStudent 2 partitionStudent 3 partitionPARTITION BY student_id๐Ÿ† Rank & SortS1: 95(rn=1), 85(rn=2)S2: 80,C20(rn=1), 80,C21(rn=2)S3: 92(rn=1)ROW_NUMBER() OVER...๐Ÿฅ‡ Select WinnersS1: C11โ†’95 โœ“S2: C20โ†’80 โœ“S3: C30โ†’92 โœ“WHERE rn = 1๐Ÿ’ก Window Function MagicROW_NUMBER() OVER (PARTITION BY student_idORDER BY grade DESC, course_id ASC) as rnโœจ Handles max grade + tie-breaking in one shot!๐ŸŽฏ Result: Each Student's Highest AchievementStudent 1 โ†’ Course 11 (Grade: 95)Student 2 โ†’ Course 20 (Grade: 80, tie-broken by course_id)Student 3 โ†’ Course 30 (Grade: 92)
Understanding the Visualization
1
Collect All Records
Gather all student enrollment records with their grades from different courses
2
Group by Student
Organize records by student using PARTITION BY student_id
3
Rank & Sort
Within each student's records, rank by grade (DESC) and course_id (ASC) for tie-breaking
4
Select Winners
Pick the top-ranked record (rn=1) for each student as their highest achievement
Key Takeaway
๐ŸŽฏ Key Insight: Window functions with proper ordering can solve complex ranking problems with tie-breaking in a single, efficient query. The ORDER BY clause in the window function does the heavy lifting: grade DESC finds the maximum, while course_id ASC breaks ties elegantly.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.2K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen