Average Review Ratings
Beginner Mode
Objective
Write a SQL query to calculate the average star rating per product by month from a product reviews table.
Table Schema:
- product_reviews
| Column | Type | Description |
|---|---|---|
| review_id | INTEGER | Unique identifier for each review |
| product_id | INTEGER | ID of the product being reviewed |
| review_date | DATETIME | Date and time when the review was submitted |
| star_rating | INTEGER | Star rating given (1-5 scale) |
Task Requirements:
- Group reviews by month and product
- Calculate the average star rating for each product in each month
- Round the average rating to the nearest integer
- Sort results by month and then by product ID
Output columns: review_month, product_id, avg_rating
Examples
Example 1:
Output:
Input:
| product_reviews | |||
|---|---|---|---|
| product_id | review_date | review_id | star_rating |
| 101 | 2024-01-15 10:00:00 | 1 | 5 |
| 101 | 2024-01-20 11:00:00 | 2 | 4 |
| 102 | 2024-01-25 12:00:00 | 3 | 3 |
| 101 | 2024-02-10 13:00:00 | 4 | 5 |
| 102 | 2024-02-15 14:00:00 | 5 | 4 |
| avg_rating | product_id | review_month |
|---|---|---|
| 5 | 101 | 2024-01-01 |
| 3 | 102 | 2024-01-01 |
| 5 | 101 | 2024-02-01 |
| 4 | 102 | 2024-02-01 |
Code Environment
Sign in or try as guest to run your code.
Track
| Question | Difficulty | Company | Access |
|---|
Need more practice in this area? Explore more questions →
Amazon