Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
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
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess 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
Build my plan
SQL Month-Over-Month Revenue and Gross Profit
00:00
5 left

SQL Month-Over-Month Revenue and Gross Profit

MediumSQL · PostgreSQL

Problem

Advance Business Capital's Operations team needs a monthly store performance view. Write a PostgreSQL query that calculates revenue, gross profit, and month-over-month changes for each store using qualifying transactions from January through March 2024.

Requirements

  1. Aggregate transactions by store and calendar month.
  2. Calculate gross profit as revenue minus cost. Treat a NULL cost as zero.
  3. Use a window function to compare each store's month with its previous available month. Return NULL changes for the first month.
  4. Exclude transactions outside the reporting period and transactions that do not match a known store. Sort by store ID and month.

Representative Data

store_transactions includes multiple transactions per store-month, a zero-revenue month, a NULL cost, an unmatched store, and a transaction outside the reporting period.

Schema

stores
ColumnTypeDescription
store_idPKINTEGERUnique store identifier
store_nameVARCHAR(100)Store display name
store_transactions
ColumnTypeDescription
transaction_idPKINTEGERUnique transaction identifier
store_idINTEGERReferenced store identifier
transaction_dateDATEDate the transaction occurred
revenueNUMERIC(12,2)Revenue generated by the transaction
costNUMERIC(12,2)Transaction cost
Tablesstoresstore_transactions
Interviewer

Your question is SQL Month-Over-Month Revenue and Gross Profit. Start with the requirements and the two tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.