Find the top 10 most-purchased Electronics products with an average rating above 4.5

Quick Overview

Write a SQL query that returns the ten most-purchased Electronics products whose average review rating is above 4.5, with ties broken by product id. It tests aggregating two one-to-many tables without join fan-out, filtering on an unrounded average, and deterministic top-N ordering.

Find the top 10 most-purchased Electronics products with an average rating above 4.5

Company: Instacart

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

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. ```hint Count the rows first Before you aggregate anything, work out how many joined rows a product with 2 orders and 3 reviews produces when both tables are joined to it at once. ``` ### 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.

Overview: Write a SQL query that returns the ten most-purchased Electronics products whose average review rating is above 4.5, with ties broken by product id. It tests aggregating two one-to-many tables without join fan-out, filtering on an unrounded average, and deterministic top-N ordering.

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

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.

columntypemeaning
product_idINTProduct identifier
product_nameVARCHARDisplay name of the product
categoryVARCHARThe 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.

columntypemeaning
order_idINTPurchase row identifier
product_idINTProduct that was bought
quantityINTUnits bought in this purchase, a positive integer

reviews: one row per review. Primary key review_id. product_id references products.product_id.

columntypemeaning
review_idINTReview identifier
product_idINTProduct that was reviewed
ratingNUMERIC(2,1)Rating between 1.0 and 5.0

Output Contract

columnmeaning
product_idProduct identifier
product_nameProduct name from products
categoryThe product's category (always 'Electronics' in the result)
total_purchasedSum of quantity over all of the product's rows in orders
avg_ratingMean 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_idproduct_namecategory
1Wireless EarbudsElectronics
2USB-C ChargerElectronics
3Smart WatchElectronics
4Coffee MakerKitchen
5Phone CaseElectronics
6HDMI CableElectronics

orders

order_idproduct_idquantity
10113
10212
10324
10421
10537
106410
10754
108620

reviews

review_idproduct_idrating
20115.0
20214.5
20324.8
20434.5
20534.5
20645.0
20754.6
20854.7
20954.9

Expected result:

product_idproduct_namecategorytotal_purchasedavg_rating
1Wireless EarbudsElectronics54.75
2USB-C ChargerElectronics54.80
5Phone CaseElectronics44.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.