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
Optimize Query on Large Database
00:00
5 left

Optimize Query on Large Database

HardSQL · PostgreSQL

Problem

How do you optimize a query on a very large database?

Assume the query must summarize completed vehicle sales from the supplied tables for calendar year 2025. Write PostgreSQL SQL that returns the requested result efficiently.

Output

  1. One row per region and qualifying vehicle model, limited to the two highest-revenue models in each non-null region.
  2. Columns: region, model_name, units_sold, and total_revenue.
  3. Sort by region ascending, revenue rank descending, and model name ascending for deterministic tie-breaking.

Schema

vehicle_sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
dealer_idINTDealer associated with the sale
model_idINTVehicle model sold
sold_atDATESale date
sale_statusVARCHAR(20)Sale lifecycle status
sale_priceNUMERIC(12,2)Sale amount
dealers
ColumnTypeDescription
dealer_idPKINTUnique dealer identifier
dealer_nameVARCHAR(100)Dealer name
regionVARCHAR(40)Sales region
vehicle_models
ColumnTypeDescription
model_idPKINTUnique model identifier
model_nameVARCHAR(80)Vehicle model name
vehicle_typeVARCHAR(40)Vehicle classification
Tablesvehicle_salesdealersvehicle_models
Interviewer

Your question is Optimize Query on Large Database. 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.