Task
A retail company needs to analyze its sales performance. Write a SQL query to calculate the month-over-month sales growth for each product category.
Requirements
- Join the
sales and products tables by product_id.
- Calculate total sales per category for each month.
- Determine the month-over-month growth percentage for each category.
- Order the results by category and month.
Schema
sales (id, product_id, sale_date, amount)
products (id, category)
| id | product_id | sale_date | amount |
|---|
| 1 | 1 | 2024-01-15 | 200 |
| 2 | 2 | 2024-01-20 | 150 |
| 3 | 1 | 2024-02-10 | 300 |
| 4 | 2 | 2024-02-15 | 200 |
| 5 | 3 | 2024-02-20 | 250 |
| id | category |
|---|
| 1 | Electronics |
| 2 | Clothing |
| 3 | Accessories |
Expected Output
| category | month | total_sales | previous_month_sales | growth_percentage |
|---|
| Electronics | 2024-01 | 200 | NULL | NULL |
| Electronics | 2024-02 | 300 | 200 | 50.00 |
| Clothing | 2024-01 | 150 | NULL | NULL |
| Clothing | 2024-02 | 200 | 150 | 33.33 |
| Accessories | 2024-02 | 250 | NULL | NULL |