Tools/- SQL

[SQL] leetcode | 3421. Find Students Who Improved ์˜ˆ์ œ ํ’€์ด

์Šค์œ„๋ฏผ 2025. 1. 24. 18:49

๋ฌธ์ œ

https://leetcode.com/problems/find-students-who-improved/

 

์ฝ”๋“œ ๋‹ต์•ˆ

with result as (
select
    student_id, 
    subject,
    lag(score) over (partition by student_id, subject order by exam_date) as first_score,
    score as latest_score
from scores
)
select *
from result
where latest_score > first_score

-- 6๋ถ„

 

๋ฌธ์ œ ํ’€์ด

  • WINDOW FUNCTION over (partition by ๊ธฐ์ค€ 1, ๊ธฐ์ค€ 2)

์ƒˆ๋กœ ์•Œ๊ฒŒ ๋œ ์‚ฌ์‹ค

group by๋„ 2๊ฐœ์˜ ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ๊ทธ๋ฃนํ™” ๊ฐ€๋Šฅํ•˜๋“ฏ์ด

partition by๋„ 2๊ฐœ์˜ ์ปฌ๋Ÿผ์„ ๊ธฐ์ค€์œผ๋กœ ํŒŒํ‹ฐ์…˜์„ ๋‚˜๋ˆ„๋Š” ๊ฒŒ ๊ฐ€๋Šฅํ•˜๋‹ค!

 

๋А๋‚€ ์ 

์—†์Œ