Find the top 10 most-purchased products in the 'Electronics' category whose average rating is greater than 4.5. Return product_id, product_name, category, total_purchased and avg_rating (rounded to 2 decimal places), ordered by total_purchased descending, then product_id ascending.
Input Table(s)
products: one row per product. Primary key product_id.
| column | type | meaning |
|---|
| product_id | INT | Product identifier |
| product_name | VARCHAR | Display name of the product |
| category | VARCHAR | The single category the product belongs to, for example 'Electronics' |
orders: one row per purchase of a product. Primary key order_id. product_id references products.product_id.
| column | type | meaning |
|---|
| order_id | INT | Purchase row identifier |
| product_id | INT | Product that was bought |
| quantity | INT | Units bought in this purchase, a positive integer |
reviews: one row per review. Primary key review_id. product_id references products.product_id.
| column | type | meaning |
|---|
| review_id | INT | Review identifier |
| product_id | INT | Product that was reviewed |
| rating | NUMERIC(2,1) | Rating between 1.0 and 5.0 |
Output Contract
| column | meaning |
|---|
| product_id | Product identifier |
| product_name | Product name from products |
| category | The product's category (always 'Electronics' in the result) |
| total_purchased | Sum of quantity over all of the product's rows in orders |
| avg_rating | Mean of the product's ratings in reviews, rounded to 2 decimal places |
Return at most 10 rows, ordered by total_purchased descending, then product_id ascending. product_id is unique, so the order is total and the cut at 10 rows is deterministic.
Example
products
| product_id | product_name | category |
|---|
| 1 | Wireless Earbuds | Electronics |
| 2 | USB-C Charger | Electronics |
| 3 | Smart Watch | Electronics |
| 4 | Coffee Maker | Kitchen |
| 5 | Phone Case | Electronics |
| 6 | HDMI Cable | Electronics |
orders
| order_id | product_id | quantity |
|---|
| 101 | 1 | 3 |
| 102 | 1 | 2 |
| 103 | 2 | 4 |
| 104 | 2 | 1 |
| 105 | 3 | 7 |
| 106 | 4 | 10 |
| 107 | 5 | 4 |
| 108 | 6 | 20 |
reviews
| review_id | product_id | rating |
|---|
| 201 | 1 | 5.0 |
| 202 | 1 | 4.5 |
| 203 | 2 | 4.8 |
| 204 | 3 | 4.5 |
| 205 | 3 | 4.5 |
| 206 | 4 | 5.0 |
| 207 | 5 | 4.6 |
| 208 | 5 | 4.7 |
| 209 | 5 | 4.9 |
Expected result:
| product_id | product_name | category | total_purchased | avg_rating |
|---|
| 1 | Wireless Earbuds | Electronics | 5 | 4.75 |
| 2 | USB-C Charger | Electronics | 5 | 4.80 |
| 5 | Phone Case | Electronics | 4 | 4.73 |
Products 1 and 2 both total 5 units, so the smaller product_id comes first. Product 3 sold 7 units but averages exactly 4.5, which is not greater than 4.5. Product 4 is in Kitchen. Product 6 sold the most units (20) but has no reviews, so it has no average rating and cannot qualify.
Constraints and Clarifications
-
products
has at most 10,000 rows;
orders
and
reviews
have at most 100,000 rows each.
-
rating
is between 1.0 and 5.0 and
quantity
is a positive integer. Neither column contains NULLs.
-
Each product belongs to exactly one
category
. Match the category name exactly as
'Electronics'
.
-
A product can have many rows in
orders
and many rows in
reviews
.
total_purchased
counts each order row once, however many reviews the product has.
-
The rating filter applies to the unrounded average. A product averaging 4.504 qualifies even though it is shown as 4.50; a product averaging exactly 4.5 does not.
-
A product appears only if it has at least one order and at least one review. If fewer than 10 products qualify, return all of them.