[LeetCode Medium] 1341. Movie Rating

2024. 1. 12. 12:32·SQL 문제 풀이

 

테이블 정보

 

 

문제

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.

 

 

결과 예시

 

 

정답 코드 (MySQL)

(SELECT u.name AS results
FROM Users u
JOIN MovieRating mr
ON u.user_id = mr.user_id
GROUP BY u.user_id
ORDER BY COUNT(mr.rating) DESC, u.name
LIMIT 1)

UNION ALL

(SELECT m.title AS results
FROM MovieRating mr
JOIN Movies m
ON mr.movie_id = m.movie_id
WHERE DATE_FORMAT(mr.created_at, '%Y-%m') = '2020-02'
GROUP BY m.title
ORDER BY AVG(mr.rating) DESC, m.title
LIMIT 1);

 

 

문제 풀이

1. UNION ALL

가장 많은 영화를 평점한 사용자의 이름을 조회하는 쿼리와

2020년 2월 평균 평점이 가장 높은 영화명을 조회하는 쿼리를

UNION ALL을 통해, 각 결과들을 합쳐준다.

 

UNION (DISTINCT) vs UNION ALL

  • UNION (DISTINCT) : 중복된 ROW는 제거하여 쿼리의 결과들을 합친다.
  • UNION ALL : 중복제거를 하지 않고 쿼리의 결과들을 합친다.

 

해당 문제의 경우, 영화의 이름과 사용자의 이름이 겹치는 경우가 존재하기 때문에

UNION ALL을 사용하여 중복되는 경우를 제거하여 출력해야 한다.

'SQL 문제 풀이' 카테고리의 다른 글

[프로그래머스 Level 4] 우유와 요거트가 담긴 장바구니  (0) 2024.01.25
[LeetCode Hard] 601. Human Traffic of Stadium  (0) 2024.01.16
[LeetCode Hard] 185. Department Top Three Salaries  (2) 2024.01.07
[LeetCode Medium] 550. Game Play Analysis IV  (0) 2024.01.05
[프로그래머스 Level 4] 입양 시각 구하기 (2)  (0) 2024.01.04
'SQL 문제 풀이' 카테고리의 다른 글
  • [프로그래머스 Level 4] 우유와 요거트가 담긴 장바구니
  • [LeetCode Hard] 601. Human Traffic of Stadium
  • [LeetCode Hard] 185. Department Top Three Salaries
  • [LeetCode Medium] 550. Game Play Analysis IV
개발이조아용
개발이조아용
IT 개발에서 배운 성장의 기록을 작성합니다.
  • 개발이조아용
    계속 하다 보면?!
    개발이조아용
  • 전체
    오늘
    어제
    • 분류 전체보기 (68)
      • Tibero DB (Tmax AI Bigdata .. (7)
      • Git (2)
      • CI CD (2)
      • Redis (3)
      • SpringBoot (16)
      • SQL 문제 풀이 (8)
      • Apache Kafka (8)
        • 오류 해결 (3)
        • 개념 정리 (4)
        • 보안 (1)
      • Nginx (3)
      • SW마에스트로 (3)
      • Kubernetes (4)
      • AWS (5)
      • gRPC (3)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Tibero
    leetcode
    KAFKA
    K8S
    MSA
    Git
    소프트웨어 마에스트로
    DynamoDB 연동
    Redis 개념
    grpc
    SASL 인증
    Kafka SASL
    Kafka 개념
    nginx
    sql 문제
    SQL
    SpringBoot
    Kafka 오류
    redis
    redis script
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
개발이조아용
[LeetCode Medium] 1341. Movie Rating
상단으로

티스토리툴바