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 gradeis never NULL- Results should be ordered by
student_idin 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code