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 student
  • subject (varchar) - Subject name (e.g., "Math", "English")
  • score (int) - Score between 0-100
  • exam_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
Student Progress AnalysisStudent 1 - Math ProgressFirst: 75Last: 82Improved โœ“Student 2 - English ProgressFirst: 90Last: 85Declined โœ—Window Function LogicFIRST_VALUE() captures starting scoreLAST_VALUE() captures latest scoreSingle query compares all student-subject pairs๐Ÿ’ก Key Insight: Window functions eliminate the need for multiple table scans
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.
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
28.5K Views
Medium Frequency
~18 min Avg. Time
1.3K 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