Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.