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
Optimize Warehouse SQL Performance
00:00
5 left

Optimize Warehouse SQL Performance

MediumSQL · PostgreSQL

Problem

How do you optimize SQL queries for performance in a warehouse environment?

Treat this as a hands-on SQL exercise. Using the provided warehouse tables, write an efficient query that preserves correctness while calculating monthly revenue metrics and comparisons.

Output

  1. One row per month, region, and product category.
  2. Columns: month_start, region, category, revenue, order_count, prior_month_revenue, revenue_change_pct, and monthly_revenue_rank.
  3. Include completed sales from January through March 2025, order by month_start, revenue descending, region, and category.

Output details

  • Use sales dates from 2025-01-01 inclusively through, but excluding, 2025-04-01.
  • Count every qualifying sales row in order_count, without deduplication.
  • Calculate prior_month_revenue within each region and category using the immediately preceding available month.
  • Return NULL for missing prior revenue and when prior_month_revenue equals zero; round revenue_change_pct to two decimal places.
  • Rank rows within each month by descending revenue, sharing ranks for ties and leaving gaps after tied ranks.

Schema

fact_sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
sale_dateDATEDate of the sale
customer_idINTPurchasing customer
product_idINTPurchased product
quantityINTUnits purchased
net_amountDECIMAL(12,2)Net sale amount
statusVARCHAR(20)Sale processing status
dim_customer
ColumnTypeDescription
customer_idPKINTCustomer identifier
regionVARCHAR(30)Customer sales region
segmentVARCHAR(30)Customer segment
dim_product
ColumnTypeDescription
product_idPKINTProduct identifier
product_nameVARCHAR(80)Product name
categoryVARCHAR(30)Product category
dim_date
ColumnTypeDescription
date_keyPKDATECalendar date
month_startDATEFirst day of the calendar month
is_business_dayBOOLEANWhether the date is a business day
Tablesfact_salesdim_customerdim_productdim_date
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the four 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.