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
Get started
Calculate Monthly Sales Growth by Product Category
00:00
5 left

Calculate Monthly Sales Growth by Product Category

MediumSQL · PostgreSQL

Problem

A retail company needs to analyze its sales performance. Write a SQL query to calculate the month-over-month sales growth for each product category.

Requirements

  1. Join the sales and products tables by product_id.
  2. Calculate total sales per category for each month.
  3. Determine the month-over-month growth percentage for each category.
  4. Order the results by category and month.

Output details

  • Return columns in this order: category, month, total_sales, previous_month_sales, and growth_percentage.
  • Format month as YYYY-MM.
  • Sort by category ascending, then month ascending.
  • Set previous_month_sales and growth_percentage to NULL for each category's first month.
  • Round growth_percentage to two decimal places.

Schema

sales
ColumnTypeDescription
idPKINTPrimary key
product_idINTForeign key referencing products
sale_dateDATEDate of the sale
amountDECIMALSale amount
products
ColumnTypeDescription
idPKINTPrimary key
categoryVARCHAR(255)Product category
Tablessalesproducts
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.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.