SQL interview question: Pivot Rows Into Columns
Pivot Sales by Month
Sales leadership wants a report showing each product's revenue pivoted by month (columns for Jan, Feb, Mar).
Transform monthly sales data from rows into columns, showing products as rows and months as columns.
Return product_name, jan_revenue, feb_revenue, and mar_revenue. Use CASE WHEN statements with SUM to pivot.
Skills: CASE WHEN with SUM for pivoting data
Tables
monthly_sales 9 rows
- id integer PK
- product_name text
- sale_month text
- revenue real
| id | product_name | sale_month | revenue |
|---|---|---|---|
| 1 | MacBook Pro | 2025-01 | 25000.0 |
| 2 | MacBook Pro | 2025-02 | 30000.0 |
| 3 | MacBook Pro | 2025-03 | 28000.0 |