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 Slow SQL at Scale
00:00
5 left

Optimize Slow SQL at Scale

HardSQL · PostgreSQL

Problem

How do you optimize a slow-running SQL query that processes millions of rows for Uber?

Treat this as a practical PostgreSQL exercise. Use the provided ride, driver, and city data to return the requested result while explaining how the query would scale.

Output

  1. One row per qualifying active driver and city, with city_name, driver_id, driver_name, completed_rides, avg_fare, and driver_rank.
  2. Include completed rides from 2025-01-01 through 2025-03-31, excluding inactive or unknown drivers and cities.
  3. Rank drivers within each city by completed rides descending, then average fare descending, and return the top three per city. Order by city_name, driver_rank, and driver_id.

Schema

rides
ColumnTypeDescription
ride_idPKINTUnique ride identifier
driver_idINTDriver assigned to the ride
city_idINTCity where the ride occurred
requested_atTIMESTAMPTime when the ride was requested
statusVARCHAR(20)Ride lifecycle status
fare_amountDECIMAL(10,2)Fare charged for the ride
drivers
ColumnTypeDescription
driver_idPKINTUnique driver identifier
driver_nameVARCHAR(100)Driver display name
is_activeBOOLEANWhether the driver is currently active
cities
ColumnTypeDescription
city_idPKINTUnique city identifier
city_nameVARCHAR(100)City display name
Tablesridesdriverscities
Interviewer

Your question is Optimize Slow SQL at Scale. 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.