Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Compare Current vs Previous Month

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Compare Current vs Previous Month. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Business Context

Avenue Code Commerce needs a regional performance report showing whether the current month is improving over the previous month.

Task

Write a PostgreSQL query that compares June 2025 metrics with May 2025 for each region. Include only completed orders placed by active customers.

Requirements

  1. Calculate monthly order count and revenue by customer region.
  2. Use a window function to retrieve the previous month's metrics for each region.
  3. Return June's metrics, May's metrics, absolute revenue change, and percentage revenue change. Return NULL for the percentage change when the previous revenue is zero.
  4. Sort the final results alphabetically by region.

Schema

customers
ColumnTypeDescription
customer_idPKINTEGERUnique customer identifier
regionVARCHAR(30)Customer sales region
emailVARCHAR(255)Customer email address
is_activeBOOLEANWhether the customer is active
orders
ColumnTypeDescription
order_idPKINTEGERUnique order identifier
customer_idINTEGERCustomer who placed the order
order_dateDATEDate the order was placed
statusVARCHAR(20)Order lifecycle status
order_amountNUMERIC(12,2)Revenue attributed to the order
Tablescustomersorders
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results