Skip to content

task: #3421 #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
74 changes: 74 additions & 0 deletions leetcode/medium/3421. Find Students Who Improved.sql
Original file line number Diff line number Diff line change
@@ -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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy