코딩테스트 SQL 코드카타 (Movie Rating)

2024. 12. 18. 10:28·Daily 코드카타/SQL

문제

 

Movie Rating - LeetCode

 

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.

 

Example 1:

Input: 
Movies table:
+-------------+--------------+
| movie_id    |  title       |
+-------------+--------------+
| 1           | Avengers     |
| 2           | Frozen 2     |
| 3           | Joker        |
+-------------+--------------+
Users table:
+-------------+--------------+
| user_id     |  name        |
+-------------+--------------+
| 1           | Daniel       |
| 2           | Monica       |
| 3           | Maria        |
| 4           | James        |
+-------------+--------------+
MovieRating table:
+-------------+--------------+--------------+-------------+
| movie_id    | user_id      | rating       | created_at  |
+-------------+--------------+--------------+-------------+
| 1           | 1            | 3            | 2020-01-12  |
| 1           | 2            | 4            | 2020-02-11  |
| 1           | 3            | 2            | 2020-02-12  |
| 1           | 4            | 1            | 2020-01-01  |
| 2           | 1            | 5            | 2020-02-17  | 
| 2           | 2            | 2            | 2020-02-01  | 
| 2           | 3            | 2            | 2020-03-01  |
| 3           | 1            | 3            | 2020-02-22  | 
| 3           | 2            | 4            | 2020-02-25  | 
+-------------+--------------+--------------+-------------+
Output: 
+--------------+
| results      |
+--------------+
| Daniel       |
| Frozen 2     |
+--------------+
Explanation: 
Daniel and Monica have rated 3 movies ("Avengers", "Frozen 2" and "Joker") 
but Daniel is smaller lexicographically.
Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is 
smaller lexicographically.

 


풀이

results 는 union으로 값을 합쳐야 한다.

  1. 사용자의 rating을 카운트 한다.
  2. max(count(raiting)) 이 사용자의 count와 동일한 사용자를 필터
  3. order by로 정렬 해주고 limit 1 으로 한명만 값을 뽑는다.
  4. 영화의 average 점수를 뽑는다.
  5. max(average) 가 영화의  average와 동일한 영화를 필터
  6. order by 로 정렬하고  limit 1 으로 1개만.

코드

with Userrating as (
    select u.name as name, count(*) as count
    from Users u
    join MovieRating m
    on u.user_id = m.user_id
    group by u.name
),
Movierating as (
    select m.title as movie, avg(mr.rating) as average
    from Movies m
    join MovieRating mr
    on m.movie_id = mr.movie_id
    where date_format(created_at,'%Y-%m') = "2020-02"
    group by title
),

Userresult as(
select name  as results
from Userrating
where count = (select max(count) from Userrating)
order by name
limit 1
),


Movieresult as(
select movie as results
from Movierating
where average = (select max(average) from Movierating)
order by movie
limit 1
)

select results from Userresult
union all
select results from Movieresult

 

Userresult와 Movieresult는 Union all 이 안되서 같이 서브쿼리화 시켰다.


링크

LeetCode/1341-movie-rating at main · K-MarkLee/LeetCode

 

LeetCode/1341-movie-rating at main · K-MarkLee/LeetCode

Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub](https://github.com/QasimWani/LeetHub) - K-MarkLee/LeetCode

github.com

 

 

저작자표시 비영리 변경금지 (새창열림)

'Daily 코드카타 > SQL' 카테고리의 다른 글

코딩테스트 SQL 코드카타 (Exchange Seats)  (0) 2024.12.17
코딩테스트 SQL 코드카타 (Last person)  (0) 2024.12.16
코딩테스트 SQL 코드카타 (Consecutive Numbers)  (1) 2024.12.12
코딩테스트 SQL 코드카타 (Primary Department)  (1) 2024.12.10
코딩테스트 SQL 코드카타 (Number of Employees)  (1) 2024.12.09
'Daily 코드카타/SQL' 카테고리의 다른 글
  • 코딩테스트 SQL 코드카타 (Exchange Seats)
  • 코딩테스트 SQL 코드카타 (Last person)
  • 코딩테스트 SQL 코드카타 (Consecutive Numbers)
  • 코딩테스트 SQL 코드카타 (Primary Department)
코드 유랑자 승열
코드 유랑자 승열
코드 유랑자 승열의 프로그래밍 일지를 남기는 공간입니다.
  • 코드 유랑자 승열
    승열의 프로그래밍 시네마
    코드 유랑자 승열
  • 전체
    오늘
    어제
  • 링크

    • 깃허브 보러가기
    • 링크드인 보러가기
    • 인스타그램 보러가기
    • 카테고리
      • 코딩테스트
        • BaekJoon
      • TIL and WIL
        • TIL
        • WIL
      • 주말스터디
      • 내일배움캠프
        • 사전캠프 강의 (SQL)
      • 용어정리
        • Python
        • Python-Library
        • Machine-Learning
        • Deep-Learning
        • AI 활용
        • LLM & RAG
        • Docker
        • Django
        • SQL
        • Java Script
        • etc
      • Daily 코드카타
        • SQL
        • Python 알고리즘
      • 임시저장
      • 보류
  • 태그

    RAG
    word2vec
    티스토리챌린지
    langchain
    django
    오블완
    template
    llm
    View
    vector db
  • 인기 글

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
코드 유랑자 승열
코딩테스트 SQL 코드카타 (Movie Rating)
상단으로

티스토리툴바