Digits
n Data
Netflix SQL Interview Question 02

Monthly
Show
Rating

Calculate the average rating for each show per month.

Schema

Table
Setup

Column NameTypeDescription
review_idintegerUnique review identifier.
review_datedateDate the review was submitted.
show_idintegerReviewed show identifier.
ratingintegerRating value.

Sample Data

Input
Output

Sample Input: reviews
review_idreview_dateshow_idrating
12024-01-01104
22024-01-02105
32024-02-01203
42024-02-02204
52024-02-03102
62024-01-05205
Expected Output
monthshow_idavg_rating
2024-01104.5
2024-01205.0
2024-02102.0
2024-02203.5

SQL Editor

Run
Query

postgresql
Waiting for query

monthshow_idavg_rating
2024-01104.5
2024-01205.0
2024-02102.0
2024-02203.5

Hints

Unlock
Clues

Hint 01: Create a month value from review_date.
Hint 02: Group by that month and show_id.
Hint 03: Use AVG(rating) as avg_rating.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT
  TO_CHAR(review_date, 'YYYY-MM') AS month,
  show_id,
  AVG(rating) AS avg_rating
FROM reviews
GROUP BY TO_CHAR(review_date, 'YYYY-MM'), show_id
ORDER BY month, show_id;

Explanation

Step By
Step

01

Convert each review_date into a month bucket.

02

Group reviews by month and show_id.

03

Average the rating values in each group.

Related Questions

Keep
Solving