Skip to content

Commit 9463e00

Browse files
committed
task: #3421
1 parent c948b7f commit 9463e00

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ Useful for preparing for technical interviews and improving your SQL skills.
183183
- [1907. Count Salary Categories](./leetcode/medium/1907.%20Count%20Salary%20Categories.sql)
184184
- [1934. Confirmation Rate](./leetcode/medium/1934.%20Confirmation%20Rate.sql)
185185
- [3220. Odd and Even Transactions](./leetcode/medium/3220.%20Odd%20and%20Even%20Transactions.sql)
186+
- [3421. Find Students Who Improved](./leetcode/medium/3421.%20Find%20Students%20Who%20Improved.sql)
186187
- [3475. DNA Pattern Recognition](./leetcode/medium/3475.%20DNA%20Pattern%20Recognition.sql)
187188
- [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql)
188189
- [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql)
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Question 3421. Find Students Who Improved
3+
Link: https://leetcode.com/problems/find-students-who-improved/description/?envType=problem-list-v2&envId=database
4+
5+
Table: Scores
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| student_id | int |
11+
| subject | varchar |
12+
| score | int |
13+
| exam_date | varchar |
14+
+-------------+---------+
15+
(student_id, subject, exam_date) is the primary key for this table.
16+
Each row contains information about a student's score in a specific subject on a particular exam date. score is between 0 and 100 (inclusive).
17+
Write a solution to find the students who have shown improvement. A student is considered to have shown improvement if they meet both of these conditions:
18+
19+
Have taken exams in the same subject on at least two different dates
20+
Their latest score in that subject is higher than their first score
21+
Return the result table ordered by student_id, subject in ascending order.
22+
*/
23+
24+
WITH first_scores AS (
25+
SELECT
26+
student_id,
27+
subject,
28+
FIRST_VALUE(score) OVER (PARTITION BY student_id, subject ORDER BY exam_date ASC) AS first_score
29+
FROM Scores
30+
),
31+
32+
last_scores AS (
33+
SELECT
34+
student_id,
35+
subject,
36+
FIRST_VALUE(score) OVER (PARTITION BY student_id, subject ORDER BY exam_date DESC) AS latest_score
37+
FROM Scores
38+
),
39+
40+
exams AS (
41+
SELECT
42+
student_id,
43+
subject
44+
FROM Scores
45+
GROUP BY student_id, subject
46+
),
47+
48+
all_scores AS (
49+
SELECT
50+
e.student_id,
51+
e.subject,
52+
(
53+
SELECT fs.first_score
54+
FROM first_scores AS fs
55+
WHERE fs.student_id = e.student_id AND fs.subject = e.subject
56+
LIMIT 1
57+
) AS first_score,
58+
(
59+
SELECT ls.latest_score
60+
FROM last_scores AS ls
61+
WHERE ls.student_id = e.student_id AND ls.subject = e.subject
62+
LIMIT 1
63+
) AS latest_score
64+
FROM exams AS e
65+
)
66+
67+
SELECT
68+
student_id,
69+
subject,
70+
first_score,
71+
latest_score
72+
FROM all_scores
73+
WHERE latest_score > first_score
74+
ORDER BY student_id, subject

0 commit comments

Comments
 (0)