Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Pivot monthly sales to columns

SQL data engineering interview problem. Difficulty: intermediate. Pattern: Pivot. About 12 minutes. Part of the Pro drill bank.

From sales(product_id, month, revenue), pivot months into columns. Return product_id, jan_revenue, feb_revenue, mar_revenue. Month labels in the seed are 'Jan', 'Feb', 'Mar'. Order by product_id.

Requirements

  • One row per product_id.

Constraints

  • Month strings are title case three-letter abbreviations.

Examples

Input: sales product_id | month | revenue P1 | Jan | 100 P1 | Feb | 120 P1 | Mar | 90 Output: product_id | jan_revenue | feb_revenue | mar_revenue P1 | 100 | 120 | 90 Why this passes: Conditional aggregation turns month rows into columns.

Topics: lakebench, sql, pivot, case.

More SQL interview questions · All interview problems · Learn data engineering

intermediate

Pivot monthly sales to columns

Interview-style drill: Pivot Jan/Feb/Mar revenue into columns.

From `sales(product_id, month, revenue)`, pivot months into columns. Return `product_id`, `jan_revenue`, `feb_revenue`, `mar_revenue`. Month labels in the seed are 'Jan', 'Feb', 'Mar'. Order by `product_id`.