KatInterview

Highest Revenue Product per Category with Returns

SQL & DataAuto-graded · 17 test cases
MediumAuto-gradedSQL & Dataaggregationjoinsranking

Last updated

Tests data aggregation, join logic, net-revenue calculation accounting for returns, and ranking with tie-breaking.

You are given four tables representing an e-commerce system: orders, order_items, products, and returns. Your task is to identify the single product in each category that generated the highest net revenue during a specific 7-day period (2025-08-26 through 2025-09-01, inclusive).

Tables

orders

  • order_id: unique order identifier
  • user_id: customer identifier
  • order_date: date the order was placed (string format YYYY-MM-DD)

order_items

  • order_id: references orders
  • product_id: references products
  • qty: quantity ordered (positive integer)

products

  • product_id: unique product identifier
  • name: product name
  • category: product category
  • price_usd: unit price in dollars (positive integer)

returns

  • return_id: unique return identifier
  • order_id: references orders
  • product_id: references products
  • qty_returned: quantity returned (positive integer)
  • return_date: date the return was processed (string format YYYY-MM-DD)

Calculation Rules

  1. Order window: Only consider orders where order_date falls between 2025-08-26 and 2025-09-01 (inclusive).

  2. Net units: For each product, calculate total units sold minus total units returned:

    • Sum all qty from order_items for orders in the date window
    • Subtract all qty_returned from returns where both the associated order is in the window AND the return_date is also in the window
    • Returns are matched by both order_id and product_id
  3. Net revenue: Multiply net units by the product's price_usd.

  4. Ranking: Within each category, select the product with the highest net revenue. If multiple products tie on net revenue:

    • Break ties by choosing the product with higher net units
    • If still tied, choose the product with the lexicographically smallest product_id

Output

Return a list of dictionaries, one per category, sorted by category name. Each dictionary must contain:

  • category: the category name
  • product_id: the winning product identifier
  • net_units: the net units sold for that product
  • net_revenue_usd: the net revenue for that product

Categories with no sales during the window should not appear in the output.

Constraints

  • 1 ≤ number of orders ≤ 10,000
  • 1 ≤ number of order_items ≤ 50,000
  • 1 ≤ number of products ≤ 1,000
  • 0 ≤ number of returns ≤ 10,000
  • All dates are valid strings in YYYY-MM-DD format
  • All quantities and prices are positive integers
  • A product may appear multiple times in the same order (multiple line items)
  • A return may reference an order outside the date window
  • Net units for a product will always be non-negative after accounting for returns

Examples

Example 1

Input: highest_revenue_per_category([{'order_id': 'O1', 'user_id': 1, 'order_date': '2025-08-26'}, {'order_id': 'O2', 'user_id': 2, 'order_date': '2025-08-27'}, {'order_id': 'O3', 'user_id': 1, 'order_date': '2025-08-30'}, {'order_id': 'O4', 'user_id': 3, 'order_date': '2025-08-31'}, {'order_id': 'O5', 'user_id': 4, 'order_date': '2025-09-01'}], [{'order_id': 'O1', 'product_id': 'P1', 'qty': 1}, {'order_id': 'O1', 'product_id': 'P2', 'qty': 2}, {'order_id': 'O2', 'product_id': 'P3', 'qty': 1}, {'order_id': 'O3', 'product_id': 'P3', 'qty': 1}, {'order_id': 'O3', 'product_id': 'P4', 'qty': 3}, {'order_id': 'O4', 'product_id': 'P2', 'qty': 1}, {'order_id': 'O5', 'product_id': 'P1', 'qty': 1}], [{'product_id': 'P1', 'name': 'Desk', 'category': 'Furniture', 'price_usd': 200}, {'product_id': 'P2', 'name': 'Chair', 'category': 'Furniture', 'price_usd': 120}, {'product_id': 'P3', 'name': 'Monitor', 'category': 'Electronics', 'price_usd': 300}, {'product_id': 'P4', 'name': 'Keyboard', 'category': 'Electronics', 'price_usd': 80}], [{'return_id': 'R1', 'order_id': 'O1', 'product_id': 'P2', 'qty_returned': 1, 'return_date': '2025-08-31'}, {'return_id': 'R2', 'order_id': 'O3', 'product_id': 'P4', 'qty_returned': 1, 'return_date': '2025-09-01'}])

Output: [{'category': 'Electronics', 'product_id': 'P3', 'net_units': 2, 'net_revenue_usd': 600}, {'category': 'Furniture', 'product_id': 'P1', 'net_units': 2, 'net_revenue_usd': 400}]

Hints

Hint 1

Aggregate order items and returns separately to the same granularity (order_id, product_id) before joining to avoid double-counting due to join fan-out.

Hint 2

Returns only reduce net units if both the original order date AND the return date fall within the 7-day window.

Hint 3

Use a three-level sort key (revenue descending, units descending, product_id ascending) to ensure deterministic tie-breaking within each category.