Monthly Sales Pivot by Product Type
SQL coding challenge · Difficulty: hard · Topic: CASE WHEN Pivoting · +200 XP
A retail company wants to analyze monthly sales performance across different product types. Given a 'sales' table, pivot the monthly sales amount for each product type into separate columns for each month. Return the product_type along with the sales for each month as separate columns.
Example input
`
sales table:
sale_id | product_type | sale_date | amount
1 | Electronics | 2024-01-05 | 250.00
2 | Electronics | 2024-02-15 | 180.00
3 | Clothing | 2024-01-18 | 320.00
4 | Clothing | 2024-03-10 | 95.00
5 | Furniture | 2024-01-22 | 410.00
6 | Electronics | 2024-03-15 | 220.00
7 | Clothing | 2024-02-20 | 75.00
`
Expected output
`
product_type | Jan | Feb | Mar
Electronics | 250.00| 180.00| 220.00
Clothing | 320.00| 75.00 | 95.00
Furniture | 410.00| 0.00 | 0.00
`
What this SQL challenge teaches you
“Monthly Sales Pivot by Product Type” is a hard-level SQL challenge focused on CASE WHEN Pivoting. Working through it gives you hands-on practice with CASE WHEN, pivot, aggregate functions, date, group by — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- CASE WHEN
- pivot
- aggregate functions
- date
- group by
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- 1 ≤ number of rows ≤ 100,000
- Each product_type can appear between 1 and 1,000 times
- sale_date values are valid dates in YYYY-MM-DD format
- amount is a positive decimal value up to two decimal places
- Return columns: product_type (TEXT), Jan (DECIMAL), Feb (DECIMAL), ..., Dec (DECIMAL) ordered by product_type ASC
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Quarterly Sales Pivot by Product
- Top 3 Products per Category
- Running Total Revenue
- Find Duplicate Emails
- Median Salary per Department
- Second Highest Salary
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated hard and covers CASE WHEN Pivoting.