Skip to content

Commit a1fab60

Browse files
authored
Create find-students-who-improved.sql
1 parent 287fb6e commit a1fab60

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

MySQL/find-students-who-improved.sql

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(nlogn)
2+
# Space: O(n)
3+
4+
# window function
5+
WITH score_cte AS (
6+
SELECT student_id,
7+
subject,
8+
FIRST_VALUE(score) OVER(PARTITION BY student_id, subject ORDER BY exam_date) AS first_score,
9+
FIRST_VALUE(score) OVER(PARTITION BY student_id, subject ORDER BY exam_date DESC) AS latest_score
10+
FROM Scores
11+
)
12+
13+
SELECT student_id,
14+
subject,
15+
first_score,
16+
latest_score
17+
FROM score_cte
18+
WHERE first_score < latest_score
19+
GROUP BY 1, 2
20+
ORDER BY 1, 2;

0 commit comments

Comments
 (0)