1 minute read

Problem

problem-1341

Note:

  • Use UNION ALL to include the the duplicate in case the two sets contain the same string. For example, the user name could be Rebecca and movie title could also be Rebecca.

Query

(
    SELECT
        Users.name AS results
    FROM
        MovieRating,
        Users
    WHERE
        Users.user_id = MovieRating.user_id
    GROUP BY
        Users.user_id
    ORDER BY
        COUNT(MovieRating.movie_id) DESC, Users.name ASC
    LIMIT 1
)

UNION ALL

(
    SELECT
        Movies.title AS results
    FROM
        Movies,
        MovieRating
    WHERE
        Movies.movie_id = MovieRating.movie_id AND
        MONTH(MovieRating.created_at) = 2 AND
        YEAR(MovieRating.created_at) = 2020
    GROUP BY
        Movies.movie_id
    ORDER BY
        AVG(MovieRating.rating) DESC, Movies.title ASC
    LIMIT 1
)

Discussion Solution

(
  SELECT u.name AS results
  FROM Movie_Rating r LEFT JOIN Users u
  ON (r.user_id = u.user_id)
  GROUP BY r.user_id
  ORDER BY COUNT(r.movie_id) DESC, u.name
  LIMIT 1
)
UNION
(
  SELECT m.title AS results
  FROM Movie_Rating r LEFT JOIN Movies m
  ON (r.movie_id = m.movie_id)
  WHERE r.created_at LIKE '2020-02%'
  GROUP BY r.movie_id
  ORDER BY AVG(r.rating) DESC, m.title
  LIMIT 1
)

Another Approach

# Write your MySQL query statement below
(SELECT name AS results
FROM MovieRating JOIN Users USING(user_id)
GROUP BY name
ORDER BY COUNT(*) DESC, name
LIMIT 1)

UNION ALL

(SELECT title AS results
FROM MovieRating JOIN Movies USING(movie_id)
WHERE EXTRACT(YEAR_MONTH FROM created_at) = 202002
GROUP BY title
ORDER BY AVG(rating) DESC, title
LIMIT 1);

Tags:

Updated: