SQL interview question: pivot monthly category revenue

MediumPivot and unpivot

Problem

Shared data rules

For each category, find revenue from orders placed in July, August and Sep 1–27, 2026, one column per month. A month with no sales is 0.

Formula

Example

Input: orders
order_idcreated_atstatusRow note
1012026-07-15 13:00delivered
1022026-08-12 18:30delivered
1032026-09-10 15:00delivered
1042026-09-28 03:00delivered← Sep 28 — outside the report
Input: order_items
order_idcategoryquantityprice_usd
101Dairy22.40
102Produce31.80
102Dairy13.00
103Produce24.00
104Dairy110.00
Output
categoryrevenue_julrevenue_augrevenue_sep
Dairy4.830
Produce05.48

Order 102 has two categories: Produce 3 × $1.80 = $5.40 and Dairy $3.00. Dairy has no sales in Sep 1–27, so its September cell is 0.

Solve it free

Your query runs on PostgreSQL in your browser. No sign-up needed.

Skills it tests

More interview questions

All questions