[SQL 문제 풀이] Find Students Who Improved (개선된 학생 찾기)

리트코드
Stupefyee's avatar
Jul 01, 2025
[SQL 문제 풀이] Find Students Who Improved (개선된 학생 찾기)
Find Students Who Improved - LeetCode
Can you solve this real interview question? Find Students Who Improved - 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. The result format is in the following example.   Example: Input: Scores table: +------------+----------+-------+------------+ | student_id | subject | score | exam_date | +------------+----------+-------+------------+ | 101 | Math | 70 | 2023-01-15 | | 101 | Math | 85 | 2023-02-15 | | 101 | Physics | 65 | 2023-01-15 | | 101 | Physics | 60 | 2023-02-15 | | 102 | Math | 80 | 2023-01-15 | | 102 | Math | 85 | 2023-02-15 | | 103 | Math | 90 | 2023-01-15 | | 104 | Physics | 75 | 2023-01-15 | | 104 | Physics | 85 | 2023-02-15 | +------------+----------+-------+------------+ Output: +------------+----------+-------------+--------------+ | student_id | subject | first_score | latest_score | +------------+----------+-------------+--------------+ | 101 | Math | 70 | 85 | | 102 | Math | 80 | 85 | | 104 | Physics | 75 | 85 | +------------+----------+-------------+--------------+ Explanation: * Student 101 in Math: Improved from 70 to 85 * Student 101 in Physics: No improvement (dropped from 65 to 60) * Student 102 in Math: Improved from 80 to 85 * Student 103 in Math: Only one exam, not eligible * Student 104 in Physics: Improved from 75 to 85 Result table is ordered by student_id, subject.
Find Students Who Improved - LeetCode
notion image
개선된 학생을 찾기 위한 솔루션을 작성합니다. 학생이 다음 두 가지 조건을 모두 충족하면 개선된 것으로 간주됩니다: * 같은 과목에서 최소 두 번의 다른 날짜에 시험을 본 적이 있습니다 * 그 과목의 최신 점수가 첫 번째 점수보다 높습니다 student_id, subject 별로 정렬된 결과 테이블을 오름차순으로 반환합니다.
 

내가 작성한 쿼리

MySQL, Oracle

-- 학생별, 과목별로 첫 점수와 마지막 점수를 구하기 위한 랭킹 부여 테이블 WITH RankedScores AS ( SELECT student_id, subject, score, exam_date, -- 시험 날짜 오름차순: 가장 빠른 시험에 1위 (첫 점수) RANK() OVER (PARTITION BY student_id, subject ORDER BY exam_date) AS first_rank, -- 시험 날짜 내림차순: 가장 최근 시험에 1위 (마지막 점수) RANK() OVER (PARTITION BY student_id, subject ORDER BY exam_date DESC) AS last_rank FROM Scores ), -- 첫 점수와 마지막 점수만 추출하는 테이블 FirstLastScores AS ( SELECT student_id, subject, -- 첫 번째 시험 점수 (first_rank = 1인 경우) MAX(CASE WHEN first_rank = 1 THEN score END) AS first_score, -- 마지막 시험 점수 (last_rank = 1인 경우) MAX(CASE WHEN last_rank = 1 THEN score END) AS latest_score, -- 시험 횟수 (같은 과목을 2번 이상 응시했는지 확인용) COUNT(*) AS exam_count FROM RankedScores GROUP BY student_id, subject ) SELECT student_id, subject, first_score, latest_score FROM FirstLastScores WHERE latest_score > first_score ORDER BY student_id, subject
Share article

stupefyee