Summarize Ratings, Review Counts, and Helpful Votes for Every Product

Quick Overview

Write a SQL query that reports each product's average rating, review count, total helpful votes and latest review date, keeping products that have no reviews. It tests left joins across three tables, avoiding join fan-out by pre-aggregating, COALESCE for empty counts, and NULL-aware multi-key ordering.

Summarize Ratings, Review Counts, and Helpful Votes for Every Product

Company: Sap

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

A product catalog stores products, the reviews customers leave on them, and the "helpful" votes other users cast on those reviews. For each product, return `product_id`, `product_name`, `avg_rating`, `total_reviews`, `total_helpful_votes` (across all of its reviews), and `latest_review_date`. Order by `avg_rating` descending, then `total_reviews` descending, then `product_id` ascending. Products with no reviews must still appear, with a NULL `avg_rating`, 0 reviews, 0 helpful votes and a NULL `latest_review_date`. ```hint Watch the row count After each join, check how many rows a single review turns into, and what that does to an average or a count. ``` ### Input Table(s) `Products` | Column | Type | Meaning | |---|---|---| | `product_id` | INT | Product identifier. Primary key. | | `product_name` | VARCHAR(100) | Product's name. Not null. | `Reviews` | Column | Type | Meaning | |---|---|---| | `review_id` | INT | Review identifier. Primary key. | | `product_id` | INT | Foreign key to `Products.product_id`. Not null. | | `user_id` | INT | User who wrote the review. Not null. | | `rating` | INT | Star rating, an integer from 1 to 5. Not null. | | `review_date` | DATE | Date the review was posted. Not null. | `HelpfulVotes` | Column | Type | Meaning | |---|---|---| | `vote_id` | INT | Vote identifier. Primary key. | | `review_id` | INT | Foreign key to `Reviews.review_id`. Not null. | | `user_id` | INT | User who marked the review as helpful. Not null. | Keys: `product_id`, `review_id` and `vote_id` are the primary keys of their tables. `(review_id, user_id)` is unique in `HelpfulVotes`. Each row in `HelpfulVotes` is one helpful vote. ### Output Contract - Columns, in this order: `product_id`, `product_name`, `avg_rating`, `total_reviews`, `total_helpful_votes`, `latest_review_date`. - Exactly one row per product in `Products`, including products with no reviews. - `avg_rating` is the mean of the product's review ratings, rounded to 2 decimal places. It is NULL when the product has no reviews. - `total_reviews` is the number of reviews of the product. `total_helpful_votes` is the number of helpful votes on all of those reviews combined. Both are 0, never NULL, when there is nothing to count. - `latest_review_date` is the most recent `review_date` of the product, or NULL when it has no reviews. - Sort by `avg_rating` (the rounded value) descending with NULLs last, then `total_reviews` descending, then `product_id` ascending. `product_id` is unique, so the order is fully determined. ### Example `Products` | product_id | product_name | |---|---| | 1 | Desk Lamp | | 2 | Office Chair | | 3 | Monitor Arm | | 4 | Keyboard | | 5 | Webcam | `Reviews` | review_id | product_id | user_id | rating | review_date | |---|---|---|---|---| | 10 | 1 | 100 | 5 | 2024-03-01 | | 11 | 1 | 101 | 3 | 2024-03-18 | | 12 | 2 | 102 | 4 | 2024-02-11 | | 13 | 3 | 100 | 5 | 2024-01-05 | | 14 | 3 | 103 | 5 | 2024-02-20 | | 15 | 3 | 104 | 4 | 2024-04-02 | | 16 | 5 | 105 | 4 | 2024-03-30 | `HelpfulVotes` | vote_id | review_id | user_id | |---|---|---| | 1 | 10 | 101 | | 2 | 10 | 102 | | 3 | 10 | 103 | | 4 | 11 | 100 | | 5 | 13 | 101 | | 6 | 15 | 100 | | 7 | 15 | 102 | Expected result: | product_id | product_name | avg_rating | total_reviews | total_helpful_votes | latest_review_date | |---|---|---|---|---|---| | 3 | Monitor Arm | 4.67 | 3 | 3 | 2024-04-02 | | 1 | Desk Lamp | 4.00 | 2 | 4 | 2024-03-18 | | 2 | Office Chair | 4.00 | 1 | 0 | 2024-02-11 | | 5 | Webcam | 4.00 | 1 | 0 | 2024-03-30 | | 4 | Keyboard | NULL | 0 | 0 | NULL | The Monitor Arm averages 14 / 3, which rounds to 4.67. The Desk Lamp, Office Chair and Webcam all average 4.00; the Desk Lamp comes first because it has two reviews, and `product_id` orders the other two. The Desk Lamp's 4 helpful votes are the 3 on review 10 plus the 1 on review 11. The Keyboard has no reviews, so it gets NULLs and zeros and sorts last. ### Constraints and Clarifications - Each table has at most 10,000 rows. - `rating` is an integer from 1 to 5. - A user votes at most once per review, so every row in `HelpfulVotes` counts as one vote. - Every review counts toward `total_reviews`, even when the same user reviewed the same product more than once. - Every `Reviews.product_id` exists in `Products`, and every `HelpfulVotes.review_id` exists in `Reviews`. - A review with no helpful votes adds 0 to `total_helpful_votes` and still counts as a review.

Overview: Write a SQL query that reports each product's average rating, review count, total helpful votes and latest review date, keeping products that have no reviews. It tests left joins across three tables, avoiding join fan-out by pre-aggregating, COALESCE for empty counts, and NULL-aware multi-key ordering.

Sap logo
Sap
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

A product catalog stores products, the reviews customers leave on them, and the "helpful" votes other users cast on those reviews. For each product, return product_id, product_name, avg_rating, total_reviews, total_helpful_votes (across all of its reviews), and latest_review_date. Order by avg_rating descending, then total_reviews descending, then product_id ascending.

Products with no reviews must still appear, with a NULL avg_rating, 0 reviews, 0 helpful votes and a NULL latest_review_date.

Input Table(s)

Products

ColumnTypeMeaning
product_idINTProduct identifier. Primary key.
product_nameVARCHAR(100)Product's name. Not null.

Reviews

ColumnTypeMeaning
review_idINTReview identifier. Primary key.
product_idINTForeign key to Products.product_id. Not null.
user_idINTUser who wrote the review. Not null.
ratingINTStar rating, an integer from 1 to 5. Not null.
review_dateDATEDate the review was posted. Not null.

HelpfulVotes

ColumnTypeMeaning
vote_idINTVote identifier. Primary key.
review_idINTForeign key to Reviews.review_id. Not null.
user_idINTUser who marked the review as helpful. Not null.

Keys: product_id, review_id and vote_id are the primary keys of their tables. (review_id, user_id) is unique in HelpfulVotes. Each row in HelpfulVotes is one helpful vote.

Output Contract

  • Columns, in this order: product_id , product_name , avg_rating , total_reviews , total_helpful_votes , latest_review_date .
  • Exactly one row per product in Products , including products with no reviews.
  • avg_rating is the mean of the product's review ratings, rounded to 2 decimal places. It is NULL when the product has no reviews.
  • total_reviews is the number of reviews of the product. total_helpful_votes is the number of helpful votes on all of those reviews combined. Both are 0, never NULL, when there is nothing to count.
  • latest_review_date is the most recent review_date of the product, or NULL when it has no reviews.
  • Sort by avg_rating (the rounded value) descending with NULLs last, then total_reviews descending, then product_id ascending. product_id is unique, so the order is fully determined.

Example

Products

product_idproduct_name
1Desk Lamp
2Office Chair
3Monitor Arm
4Keyboard
5Webcam

Reviews

review_idproduct_iduser_idratingreview_date
10110052024-03-01
11110132024-03-18
12210242024-02-11
13310052024-01-05
14310352024-02-20
15310442024-04-02
16510542024-03-30

HelpfulVotes

vote_idreview_iduser_id
110101
210102
310103
411100
513101
615100
715102

Expected result:

product_idproduct_nameavg_ratingtotal_reviewstotal_helpful_voteslatest_review_date
3Monitor Arm4.67332024-04-02
1Desk Lamp4.00242024-03-18
2Office Chair4.00102024-02-11
5Webcam4.00102024-03-30
4KeyboardNULL00NULL

The Monitor Arm averages 14 / 3, which rounds to 4.67. The Desk Lamp, Office Chair and Webcam all average 4.00; the Desk Lamp comes first because it has two reviews, and product_id orders the other two. The Desk Lamp's 4 helpful votes are the 3 on review 10 plus the 1 on review 11. The Keyboard has no reviews, so it gets NULLs and zeros and sorts last.

Constraints and Clarifications

  • Each table has at most 10,000 rows.
  • rating is an integer from 1 to 5.
  • A user votes at most once per review, so every row in HelpfulVotes counts as one vote.
  • Every review counts toward total_reviews , even when the same user reviewed the same product more than once.
  • Every Reviews.product_id exists in Products , and every HelpfulVotes.review_id exists in Reviews .
  • A review with no helpful votes adds 0 to total_helpful_votes and still counts as a review.