Find Students Who Improved - Problem
You're working for an educational analytics company that tracks student performance over time. Your task is to identify students who have shown meaningful improvement in their academic journey.
The Challenge: Given a database table containing student exam scores across different subjects and dates, find all students who have demonstrated clear improvement in at least one subject.
What constitutes improvement?
- The student must have taken at least 2 exams in the same subject on different dates
- Their latest score in that subject must be higher than their first score
Input: A Scores table with columns:
student_id(int) - Unique identifier for each studentsubject(varchar) - Subject name (e.g., "Math", "English")score(int) - Score between 0-100exam_date(varchar) - Date of the exam
Output: Return all students who improved, ordered by student_id and subject in ascending order.
Input & Output
basic_improvement.sql โ Basic Case
$
Input:
Scores table:
| student_id | subject | score | exam_date |
|------------|---------|-------|------------|
| 1 | Math | 75 | 2023-01-15 |
| 1 | Math | 82 | 2023-03-20 |
| 2 | English | 90 | 2023-02-10 |
| 2 | English | 85 | 2023-04-15 |
โบ
Output:
| student_id | subject |
|------------|----------|
| 1 | Math |
๐ก Note:
Student 1 improved in Math (75 โ 82), while Student 2 declined in English (90 โ 85). Only Student 1 shows improvement.
multiple_subjects.sql โ Multiple Subjects
$
Input:
Scores table:
| student_id | subject | score | exam_date |
|------------|---------|-------|------------|
| 1 | Math | 70 | 2023-01-10 |
| 1 | Math | 85 | 2023-02-15 |
| 1 | English | 60 | 2023-01-12 |
| 1 | English | 75 | 2023-02-18 |
โบ
Output:
| student_id | subject |
|------------|----------|
| 1 | English |
| 1 | Math |
๐ก Note:
Student 1 improved in both subjects: Math (70 โ 85) and English (60 โ 75). Results are ordered by subject name.
single_exam.sql โ Edge Case
$
Input:
Scores table:
| student_id | subject | score | exam_date |
|------------|---------|-------|------------|
| 1 | Math | 85 | 2023-01-15 |
| 2 | English | 70 | 2023-02-10 |
| 2 | English | 80 | 2023-03-15 |
โบ
Output:
| student_id | subject |
|------------|----------|
| 2 | English |
๐ก Note:
Student 1 has only one Math exam, so can't show improvement. Student 2 improved in English (70 โ 80) with two exams.
Constraints
- 1 โค student_id โค 105
- 1 โค score โค 100
- Subject names are non-empty strings
- Each exam_date is a valid date string
- At least 2 exams in the same subject required for improvement consideration
Visualization
Tap to expand
Understanding the Visualization
1
Group by Student-Subject
Create separate progress charts for each student-subject combination
2
Time-Order Scores
Arrange scores chronologically to see the progression timeline
3
Identify First & Last
Mark the starting point and current position for each progress chart
4
Calculate Improvement
Compare endpoints to determine if there's upward trajectory
Key Takeaway
๐ฏ Key Insight: Window functions like FIRST_VALUE and LAST_VALUE allow us to compare temporal data points efficiently within each partition, making student progress analysis both elegant and performant.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code