logo

Back to questions

Best-Selling Product

Medium

Write an SQL query to find the best-selling product in each product category. If there are two or more products with the same sales quantity, go by whichever product which has the higher review rating.

Return the category name and product name in alphabetical order of the category.

Table:

Column NameType
product_idinteger
product_namevarchar
category_namevarchar

Example Input:

product_idproduct_namecategory_name
3690Game of ThronesBooks
5520RefrigeratorHome Appliances
5952DishwasherHome Appliances
3561IKGAIBooks

Table:

Column NameType
product_idinteger
sales_quantityinteger
ratingdecimal (1.0 - 5.0)

Example Input:

product_idsales_quantityrating
36903004.9
5520703.8
5952704.0
35612904.5

Example Output:

category_nameproduct_name
BooksGame of Thrones
Home AppliancesDishwasher

PostgreSQL 14

Refer friends to get bonus content & cool prizes.