Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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 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
Build my plan
Optimize SQL Query Performance
00:00
5 left

Optimize SQL Query Performance

HardSQL · PostgreSQL

Problem

How do you optimize SQL queries for performance?

Demonstrate your approach with a PostgreSQL query over the supplied tables. Return monthly revenue by product category for completed orders, including the previous available month, percentage change, and a three-month rolling revenue total.

Output

  1. One row per category and revenue month, with month, category, monthly_revenue, previous_month_revenue, revenue_change_percent, and rolling_three_month_revenue.
  2. Include only completed orders from January through April 2025. Sort by category, then month.

Schema

orders
ColumnTypeDescription
order_idPKINTUnique order identifier
order_dateDATEOrder creation date
statusVARCHAR(20)Order processing status
order_items
ColumnTypeDescription
order_item_idPKINTUnique order-item identifier
order_idINTReferenced order
product_idINTReferenced product
quantityINTNumber of units
unit_priceNUMERIC(12,2)Price per unit
products
ColumnTypeDescription
product_idPKINTUnique product identifier
product_nameVARCHAR(100)Product name
categoryVARCHAR(50)Product category
Tablesordersorder_itemsproducts
Interviewer

Your question is Optimize SQL Query Performance. Start with the requirements and the three 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.