분류 전체보기 185

[LeetCode] movie-rating

https://leetcode.com/problems/movie-rating/submissions/1213326562/?envType=study-plan-v2&envId=top-sql-50 맨 처음 작성한 쿼리는 다음과 같다. -- 가장 많은 리뷰 남긴 사람 (SELECT name AS results FROM ( SELECT u.name, COUNT(r.movie_id) AS cnt FROM MovieRating r JOIN Users u ON r.user_id = u.user_id GROUP BY u.name ORDER BY cnt DESC, u.name ) a LIMIT 1) UNION -- Feb, 2020에 평균 평점 가장 높은 영화 (SELECT title AS results FROM ( SEL..

카테고리 없음 2024.03.25

[LeetCode] exchange-seats 📌

https://leetcode.com/problems/exchange-seats/?envType=study-plan-v2&envId=top-sql-50 SELECT a.id, CASE WHEN a.id % 2 = 1 THEN b.student ELSE c.student END AS student FROM seat a LEFT JOIN seat b ON a.id + 1 = b.id LEFT JOIN seat c ON a.id = c.id + 1 id가 홀수인 경우와 짝수인 경우를 나눠서 문제를 해결하려고 했는데 table의 마지막 열에 대해서 어떻게 처리가 안되더라 .. 원래 table은 다음과 같다. SELECT CASE WHEN id = (SELECT MAX(id) FROM seat) AND id % ..

SQL/문제풀이 2024.03.20

[LeetCode] last-person-to-fit-in-the-bus 📌

https://leetcode.com/problems/last-person-to-fit-in-the-bus/?envType=study-plan-v2&envId=top-sql-50 누적합을 어떻게 구해야하는지 몰라서 문제에 접근을 못했다! 1. JOIN 이용 SELECT * FROM Queue a JOIN Queue b ON a.turn >= b.turn ORDER BY a.turn, b.turn ex) a의 turn이 2 --> turn이 1, 2인 row가 JOIN 됨 2. a.turn으로 GROUP BY 후 해당 순서의 누적 weight를 구하기 위해 SUM(b.weight)를 계산 HAVING절에 1000 이하라는 조건을 걸어주고 누적 weight가 가장 큰 순서대로 정렬한다 이때 a.person_..

SQL/문제풀이 2024.03.20