Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Pivot Monthly Sales by Category for Dashboard Reporting

Medium
SQL & Data ManipulationData WranglingAsked 7 times

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

  1. Output one row per month in the range, with months formatted as YYYY-MM.
  2. Columns for total sales of 'Electronics', 'Clothing', and 'Books'.
  3. If a category has no sales in a month, show NULL for that column.
  4. Order results by month ascending.

Schema

sales
ColumnTypeDescription
idPKINTPrimary key for the sales record
categoryVARCHAR(255)Product category for the sale
sale_dateDATEDate of the sale
amountDECIMAL(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.

Sign up freeI have an account
SELECT ...
FROM ...
JOIN ... ON ...
GROUP BY ...
HAVING ...
ORDER BY ... DESC;
Sign up to unlock solutions
Trader Joe's Business Analyst Interview QuestionsRevature QA Engineer Interview QuestionsDICK'S Sporting Goods Data Analyst Interview QuestionsRevature Interview QuestionsRegenstrief Institute Data Analyst Interview Questions
Next questions
WalgreensMonthly Sales Aggregation by Product CategoryMediumTotal Wine & MoreMonthly Sales Trends by CategoryMediumWayfairCalculate Monthly Sales Growth by Product CategoryMedium
PostgreSQL