Nestlé USA marketing analysts need a category-level view of product performance across brands such as NESCAFÉ, KITKAT, and Stouffer’s. Write a PostgreSQL query that returns the three highest-selling products within every category.
Requirements
- Join the category, product, and sales tables.
- Calculate total sales for each product using
sales_amount.
- Rank products independently within each category, with the highest total sales ranked first.
- Return only ranks 1 through 3, ordered by category and rank. Treat NULL sales amounts as zero when calculating totals.
Use ROW_NUMBER() so ties are resolved deterministically by the smaller product_id.