diff --git a/README.md b/README.md index c9b7f7a..619adf2 100644 --- a/README.md +++ b/README.md @@ -183,6 +183,7 @@ Useful for preparing for technical interviews and improving your SQL skills. - [1907. Count Salary Categories](./leetcode/medium/1907.%20Count%20Salary%20Categories.sql) - [1934. Confirmation Rate](./leetcode/medium/1934.%20Confirmation%20Rate.sql) - [3220. Odd and Even Transactions](./leetcode/medium/3220.%20Odd%20and%20Even%20Transactions.sql) + - [3421. Find Students Who Improved](./leetcode/medium/3421.%20Find%20Students%20Who%20Improved.sql) - [3475. DNA Pattern Recognition](./leetcode/medium/3475.%20DNA%20Pattern%20Recognition.sql) - [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql) - [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql) diff --git a/leetcode/medium/3421. Find Students Who Improved.sql b/leetcode/medium/3421. Find Students Who Improved.sql new file mode 100644 index 0000000..0c90852 --- /dev/null +++ b/leetcode/medium/3421. Find Students Who Improved.sql @@ -0,0 +1,74 @@ +/* +Question 3421. Find Students Who Improved +Link: https://leetcode.com/problems/find-students-who-improved/description/?envType=problem-list-v2&envId=database + +Table: Scores + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| student_id | int | +| subject | varchar | +| score | int | +| exam_date | varchar | ++-------------+---------+ +(student_id, subject, exam_date) is the primary key for this table. +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). +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: + +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. +*/ + +WITH first_scores AS ( + SELECT + student_id, + subject, + FIRST_VALUE(score) OVER (PARTITION BY student_id, subject ORDER BY exam_date ASC) AS first_score + FROM Scores +), + +last_scores AS ( + SELECT + student_id, + subject, + FIRST_VALUE(score) OVER (PARTITION BY student_id, subject ORDER BY exam_date DESC) AS latest_score + FROM Scores +), + +exams AS ( + SELECT + student_id, + subject + FROM Scores + GROUP BY student_id, subject +), + +all_scores AS ( + SELECT + e.student_id, + e.subject, + ( + SELECT fs.first_score + FROM first_scores AS fs + WHERE fs.student_id = e.student_id AND fs.subject = e.subject + LIMIT 1 + ) AS first_score, + ( + SELECT ls.latest_score + FROM last_scores AS ls + WHERE ls.student_id = e.student_id AND ls.subject = e.subject + LIMIT 1 + ) AS latest_score + FROM exams AS e +) + +SELECT + student_id, + subject, + first_score, + latest_score +FROM all_scores +WHERE latest_score > first_score +ORDER BY student_id, subject