420. Top Customer per Product Category

Medium Marketing Customer Segmentation
Query Rules
  • The query columns and order must match the expected result schema.
  • NULL values should be handled appropriately where applicable.
  • The grader compares output values and sorting order to evaluate.

Schema Browser

Click a table's header to toggle expand/collapse.
locations
3 cols
departments
3 cols
employees
6 cols
orders
6 cols

Reference Solution

Expected SQL
SELECT product_category, customer_name, total_spent FROM (SELECT product_category, customer_name, SUM(amount) AS total_spent, ROW_NUMBER() OVER (PARTITION BY product_category ORDER BY SUM(amount) DESC) AS rn FROM orders GROUP BY product_category, customer_name) sub WHERE rn = 1 ORDER BY product_category;
SQL Editor
Ctrl+Enter to run
Console Output

Console Terminal

Write your query and click "Run Query" (Ctrl + Enter) to see results and testcase validation.