Business Context
Cloud Software Group's product analytics team wants to identify the strongest-selling products within each product category. Revenue should reflect completed orders only.
Task
Write a PostgreSQL query that returns the top three products by revenue for every product category.
Requirements
- Join products, order items, and orders to calculate revenue as
quantity * unit_price.
- Include only orders with
status = 'completed'.
- Aggregate revenue by product and category, then rank products independently within each category.
- Return products ranked 1 through 3, ordered by category, revenue descending, and product ID as a deterministic tie-breaker.