ror_coding

[LeetCode] 1341. Movie Rating 본문

Coding Test/SQL

[LeetCode] 1341. Movie Rating

ro_rdil_31 2025. 7. 19. 14:37
728x90

문제를 잘 읽자!!! subquery를 잘 활용하자..!

 

Question

 

Write a solution to:

  • Find the name of the user who has rated the greatest number of movies. In case of a tie, return the lexicographically smaller user name.
  • Find the movie name with the highest average rating in February 2020. In case of a tie, return the lexicographically smaller movie name.

The result format is in the following example.

 

Code

 

select name as results
from users
where user_id = (
    select user_id
    from movierating
    join users using(user_id)
    group by 1
    order by count(*) desc, name
    limit 1
)

union all

select title as results
from movies
where movie_id = (
    select movie_id
    from movierating
    join movies using(movie_id)
    where created_at like '2020-02%'
    group by 1
    order by avg(rating) desc, title
    limit 1
)

 

My Code
728x90

'Coding Test > SQL' 카테고리의 다른 글

[LeetCode] 1321. Restaurant Growth  (0) 2025.07.19
[LeetCode] 626. Exchange Seats  (0) 2025.07.19
[LeetCode] 1907. Count Salary Categories  (1) 2025.07.19
[LeetCode] 1934. Confirmation Rate  (0) 2025.07.13
[LeetCode] 176. Second Highest Salary  (0) 2025.07.13