Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Compare Current vs Previous Month
00:00
5 left

Compare Current vs Previous Month

MediumSQL · PostgreSQL

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.

Output details

  • Return columns in this order: region, current_month, previous_month, current_orders, previous_orders, current_revenue, previous_revenue, revenue_change, revenue_change_pct.
  • Format current_month and previous_month as YYYY-MM strings.
  • Round revenue_change_pct to two decimal places.
  • Return NULL for revenue_change_pct when previous_revenue is NULL, as well as when it is zero.

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
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.