Highest-Grossing Items
Amazon ☯️ Medium SQL
Beginner Mode

Objective

Write a SQL query to find the top 2 highest-grossing products within each category for the year 2022.

Table Schema:

  • product_spend
Column Type Description
transaction_id INTEGER Unique identifier for each transaction
product_id INTEGER ID of the product purchased
category VARCHAR Product category (e.g., Electronics, Clothing, Books)
product VARCHAR Name of the product
spend DECIMAL Amount spent on the product in this transaction
transaction_date DATE Date when the transaction occurred

Task Requirements:

  • Filter transactions for the year 2022
  • Calculate total spend for each product within each category
  • Identify the top 2 products by total spend in each category
  • Order results by category and total spend (descending)

Output columns: category, product, total_spend

Examples

Example 1:

Input:

product_spend
categoryproductproduct_idspendtransaction_datetransaction_id
ElectronicsLaptop10112002022-01-151
ElectronicsLaptop10112002022-02-202
ElectronicsHeadphones1021502022-03-103
ElectronicsMouse103252022-04-054
ClothingJacket201802022-05-125
ClothingShoes2021202022-06-186

Output:
categoryproducttotal_spend
ClothingShoes120
ClothingJacket80
ElectronicsLaptop2400
ElectronicsHeadphones150
Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →