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
Optimizing Large-Scale SQL
00:00
5 left

Optimizing Large-Scale SQL

HardSQL · PostgreSQL

Problem

How do you optimize complex SQL queries for large-scale datasets?

Write a PostgreSQL query using the provided tables to calculate customer revenue by month, compare it with each customer's previous reported month, and rank customers within their region and month. Include only completed orders in 2024 and active products.

Output

  1. One row per customer-month with customer_name, region, month_start, monthly_revenue, prior_month_revenue, revenue_change, and regional_rank.
  2. Include only customer-months with revenue of at least 1,000.
  3. Order by month_start, region, regional_rank, and customer_name.

Schema

customers
ColumnTypeDescription
customer_idPKINTCustomer identifier
customer_nameVARCHAR(100)Customer display name
regionVARCHAR(30)Customer sales region
orders
ColumnTypeDescription
order_idPKINTOrder identifier
customer_idINTCustomer who placed the order
order_dateDATEOrder creation date
statusVARCHAR(20)Order processing status
order_items
ColumnTypeDescription
order_item_idPKINTOrder line identifier
order_idINTParent order identifier
product_idINTPurchased product identifier
quantityINTUnits purchased
unit_priceNUMERIC(10,2)Price per unit at purchase time
products
ColumnTypeDescription
product_idPKINTProduct identifier
product_nameVARCHAR(100)Product name
categoryVARCHAR(50)Product category
is_activeBOOLEANWhether the product is currently active
Tablescustomersordersorder_itemsproducts
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.

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.