Back to questions

Highest-Grossing Items Amazon SQL Interview Question

Highest-Grossing Items

Amazon SQL Interview Question

This is the same question as problem #12 in the SQL Chapter of Ace the Data Science Interview!

Assume you're given a table containing data on Amazon customers and their spending on products in different category, write a query to identify the top two highest-grossing products within each category in the year 2022. The output should include the category, product, and total spend.

Table:

Column NameType
categorystring
productstring
user_idinteger
spenddecimal
transaction_datetimestamp

Example Input:

categoryproductuser_idspendtransaction_date
appliancerefrigerator165246.0012/26/2021 12:00:00
appliancerefrigerator123299.9903/02/2022 12:00:00
appliancewashing machine123219.8003/02/2022 12:00:00
electronicsvacuum178152.0004/05/2022 12:00:00
electronicswireless headset156249.9007/08/2022 12:00:00
electronicsvacuum145189.0007/15/2022 12:00:00

Example Output:

categoryproducttotal_spend
appliancerefrigerator299.99
appliancewashing machine219.80
electronicsvacuum341.00
electronicswireless headset249.90

Explanation:

Within the "appliance" category, the top two highest-grossing products are "refrigerator" and "washing machine."

In the "electronics" category, the top two highest-grossing products are "vacuum" and "wireless headset."

The dataset you are querying against may have different input & output - this is just an example!

Input

Output