MEDIUMVerified Real Interview• Data Analyst

Highest-Grossing Items in Each Category

You are given a table of product transactions from Amazon's retail store in year 2026.

Write a query to identify the top 2 highest-grossing products within each category. For each product, output:

  • category
  • product
  • total_spend

Results should be ordered by category ascending, and then by total_spend descending.

Table:

  • product_spend (category VARCHAR, product VARCHAR, user_id INT, spend DECIMAL, transaction_date TIMESTAMP)

Execution Sandbox:

Your query executes inside an isolated in-memory PostgreSQL engine (PGlite WebAssembly). You have full access to standard PostgreSQL aggregations, window functions, and CTEs.

Standard SQL (ANSI)|Ln 1, Col 1
⌘↵ Run Query
Ready to execute. Press Run (⌘+Enter)
Write your query and click “Run” or press ⌘+Enter to inspect output.