You are given a table Scores that contains information about students' exam scores in different subjects over multiple dates.
Table Schema:
| Column Name | Type |
|---|---|
| student_id | int |
| subject | varchar |
| score | int |
| exam_date | varchar |
Primary Key: (student_id, subject, exam_date)
Each row contains information about a student's score in a specific subject on a particular exam date. The score is between 0 and 100 (inclusive).
Task: Write a SQL query to find students who have shown improvement. A student is considered to have shown improvement if they meet both conditions:
- Have taken exams in the same subject on at least two different dates
- Their latest score in that subject is higher than their first score
Return the result table ordered by student_id, subject in ascending order.
Table Schema
| Column Name | Type | Description |
|---|---|---|
student_id
PK
|
int | Unique identifier for each student |
subject
PK
|
varchar | Subject name (e.g., Math, Science) |
score
|
int | Exam score between 0 and 100 |
exam_date
PK
|
varchar | Date when the exam was taken |
Input & Output
| student_id | subject | score | exam_date |
|---|---|---|---|
| 1 | Math | 70 | 2023-01-15 |
| 1 | Math | 85 | 2023-03-20 |
| 1 | Physics | 90 | 2023-01-10 |
| 2 | Math | 80 | 2023-01-12 |
| 2 | Math | 75 | 2023-02-15 |
| 3 | Physics | 65 | 2023-01-08 |
| student_id | subject |
|---|---|
| 1 | Math |
Student 1 improved in Math (70 → 85) and took multiple exams. Student 1 only took Physics once, so no comparison possible. Student 2 declined in Math (80 → 75). Student 3 only took Physics once.
| student_id | subject | score | exam_date |
|---|---|---|---|
| 1 | Math | 60 | 2023-01-01 |
| 1 | Math | 80 | 2023-02-01 |
| 1 | Science | 70 | 2023-01-05 |
| 1 | Science | 90 | 2023-02-05 |
| 2 | Math | 85 | 2023-01-10 |
| 2 | Math | 95 | 2023-03-10 |
| student_id | subject |
|---|---|
| 1 | Math |
| 1 | Science |
| 2 | Math |
Student 1 improved in both Math (60 → 80) and Science (70 → 90). Student 2 improved in Math (85 → 95). All results ordered by student_id, subject.
Constraints
-
1 ≤ student_id ≤ 1000 -
1 ≤ score ≤ 100 -
subjectcontains only letters and spaces -
exam_dateis in valid date format