Problem
Scenario
TechMart tracks all sales in a single sales table. The analytics team needs a dashboard that shows, for each month, the total sales amount for each product category as separate columns.
Task
Write a SQL query to pivot the sales data so that each row represents a month (in YYYY-MM format), and each column shows the total sales for a specific category. Only include sales from January to March 2024 (inclusive).
Requirements
- Output one row per month in the range, with months formatted as
YYYY-MM. - Columns for total sales of 'Electronics', 'Clothing', and 'Books'.
- If a category has no sales in a month, show
NULLfor that column. - Order results by month ascending.
Schema
sales
| Column | Type | Description |
|---|---|---|
| idPK | INT | Primary key for the sales record |
| category | VARCHAR(255) | Product category for the sale |
| sale_date | DATE | Date of the sale |
| amount | DECIMAL(10,2) | Amount of the sale |
You are practicing as a guest. Sign up free to run your code against the sample data. Your draft stays right here.


