Company: Shopify | Difficulty: easy
Calculate the average review rating for each product.
Return the columns in this exact order: product_id and avg_rating. Round the average rating to two decimal places and sort the results by product_id in descending order.
product_id
avg_rating
Reviews with NULL ratings should be ignored when calculating the average.
NULL
product_reviews
review_id
rating
Product 5001 has an average rating of 4.50 from ratings of 5.0 and 4.0. Product 5002 has one valid rating of 3.0 (returned as 3.00); its NULL rating is ignored. The output is sorted by product_id in descending order, meaning 5002 will appear before 5001.
5001
4.50
5.0
4.0
5002
3.0
3.00
DROP TABLE IF EXISTS product_reviews; CREATE TABLE product_reviews ( review_id INT, product_id INT, rating DECIMAL(2,1) ); INSERT INTO product_reviews VALUES (1, 5001, 5.0), (2, 5001, 4.0), (3, 5002, 3.0), (4, 5002, NULL);
← All SQL Challenges · Shopify Questions · Learn SQL Free