Average Product Rating per Month in 2019

Quick Overview

Write a SQL query that reports, for every product and every month of 2019, the average star rating rounded to two decimals, with the month shown as a YYYY-MM string. It tests year filtering with half-open date ranges, month formatting, grouping on two keys, and averaging an integer column.

Average Product Rating per Month in 2019

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `reviews` table stores one row per product review. For each product and each month of 2019, return `month` as a `YYYY-MM` string, `product_id`, and `avg_stars`, the average `stars` rounded to 2 decimal places. Use `created_at` to decide which month (and year) a review belongs to. Order the result by `month`, then `product_id`, both ascending. ```hint Year boundaries Check that your 2019 filter keeps a review created at 23:30 on December 31 and drops one created at midnight on January 1, 2020. ``` ### Input Table(s) `reviews` | Column | Type | Meaning | |---|---|---| | review_id | INT | Unique identifier of the review | | user_id | INT | User who wrote the review, never NULL | | submit_date | DATETIME (TIMESTAMP in PostgreSQL) | When the user submitted the review, never NULL; not used for this task | | product_id | INT | Product being reviewed, never NULL | | stars | INT | Rating from 1 to 5, never NULL | | created_at | DATETIME (TIMESTAMP in PostgreSQL) | When the review record was created, never NULL; defines the review's month | Primary key: `review_id`. ### Output Contract | Column | Type | Meaning | |---|---|---| | month | TEXT | Year and month of `created_at` in `YYYY-MM` form, for example `2019-03` | | product_id | INT | The reviewed product | | avg_stars | DECIMAL | Average `stars` of that product's reviews in that month, rounded to 2 decimal places | - One row per (`month`, `product_id`) pair that has at least one review created in 2019. - Rows are ordered by `month` ascending, then `product_id` ascending. Each pair appears once, so this order is unique. ### Example `reviews` | review_id | user_id | submit_date | product_id | stars | created_at | |---|---|---|---|---|---| | 1 | 11 | 2018-12-31 23:59:00 | 50001 | 1 | 2018-12-31 23:59:00 | | 2 | 12 | 2019-01-04 09:15:00 | 50001 | 4 | 2019-01-04 09:15:00 | | 3 | 13 | 2019-01-10 14:00:00 | 50001 | 5 | 2019-01-10 14:00:00 | | 4 | 14 | 2019-01-22 08:30:00 | 50001 | 5 | 2019-01-22 08:30:00 | | 5 | 15 | 2019-01-18 19:45:00 | 69852 | 3 | 2019-01-18 19:45:00 | | 6 | 16 | 2019-01-31 23:58:00 | 69852 | 2 | 2019-02-01 00:01:00 | | 7 | 17 | 2019-02-14 12:00:00 | 69852 | 3 | 2019-02-14 12:00:00 | | 8 | 18 | 2019-12-31 23:30:00 | 50001 | 4 | 2019-12-31 23:30:00 | | 9 | 19 | 2020-01-01 00:00:00 | 69852 | 5 | 2020-01-01 00:00:00 | Expected output: | month | product_id | avg_stars | |---|---|---| | 2019-01 | 50001 | 4.67 | | 2019-01 | 69852 | 3.00 | | 2019-02 | 69852 | 2.50 | | 2019-12 | 50001 | 4.00 | Reviews 1 and 9 fall outside 2019 and are dropped. Product 50001 has three January reviews, (4 + 5 + 5) / 3 = 4.666..., which rounds to 4.67. Review 6 was submitted in January but created on February 1, so it counts in `2019-02`, where product 69852 averages (2 + 3) / 2 = 2.50. Review 8, created late on December 31, still counts in `2019-12`. ### Constraints and Clarifications - `reviews` has at most 10,000 rows, and `review_id` is unique. - `stars` is an integer from 1 to 5. No column is NULL. - `created_at` spans several years. Only reviews whose `created_at` falls in calendar year 2019 are included. - Timestamps are compared as stored, with no time zone conversion. - A product with no reviews in a given month has no row for that month.

Overview: Write a SQL query that reports, for every product and every month of 2019, the average star rating rounded to two decimals, with the month shown as a YYYY-MM string. It tests year filtering with half-open date ranges, month formatting, grouping on two keys, and averaging an integer column.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The reviews table stores one row per product review. For each product and each month of 2019, return month as a YYYY-MM string, product_id, and avg_stars, the average stars rounded to 2 decimal places. Use created_at to decide which month (and year) a review belongs to. Order the result by month, then product_id, both ascending.

Input Table(s)

reviews

ColumnTypeMeaning
review_idINTUnique identifier of the review
user_idINTUser who wrote the review, never NULL
submit_dateDATETIME (TIMESTAMP in PostgreSQL)When the user submitted the review, never NULL; not used for this task
product_idINTProduct being reviewed, never NULL
starsINTRating from 1 to 5, never NULL
created_atDATETIME (TIMESTAMP in PostgreSQL)When the review record was created, never NULL; defines the review's month

Primary key: review_id.

Output Contract

ColumnTypeMeaning
monthTEXTYear and month of created_at in YYYY-MM form, for example 2019-03
product_idINTThe reviewed product
avg_starsDECIMALAverage stars of that product's reviews in that month, rounded to 2 decimal places
  • One row per ( month , product_id ) pair that has at least one review created in 2019.
  • Rows are ordered by month ascending, then product_id ascending. Each pair appears once, so this order is unique.

Example

reviews

review_iduser_idsubmit_dateproduct_idstarscreated_at
1112018-12-31 23:59:005000112018-12-31 23:59:00
2122019-01-04 09:15:005000142019-01-04 09:15:00
3132019-01-10 14:00:005000152019-01-10 14:00:00
4142019-01-22 08:30:005000152019-01-22 08:30:00
5152019-01-18 19:45:006985232019-01-18 19:45:00
6162019-01-31 23:58:006985222019-02-01 00:01:00
7172019-02-14 12:00:006985232019-02-14 12:00:00
8182019-12-31 23:30:005000142019-12-31 23:30:00
9192020-01-01 00:00:006985252020-01-01 00:00:00

Expected output:

monthproduct_idavg_stars
2019-01500014.67
2019-01698523.00
2019-02698522.50
2019-12500014.00

Reviews 1 and 9 fall outside 2019 and are dropped. Product 50001 has three January reviews, (4 + 5 + 5) / 3 = 4.666..., which rounds to 4.67. Review 6 was submitted in January but created on February 1, so it counts in 2019-02, where product 69852 averages (2 + 3) / 2 = 2.50. Review 8, created late on December 31, still counts in 2019-12.

Constraints and Clarifications

  • reviews has at most 10,000 rows, and review_id is unique.
  • stars is an integer from 1 to 5. No column is NULL.
  • created_at spans several years. Only reviews whose created_at falls in calendar year 2019 are included.
  • Timestamps are compared as stored, with no time zone conversion.
  • A product with no reviews in a given month has no row for that month.