Dataford
Interview Guides
Upgrade
All questions/SQL & Data Manipulation/Top Driver Earnings Per Hour

Top Driver Earnings Per Hour

Medium
SQL & Data Manipulation
Asked at 3 companies3AggregationsRankingDate Functions
Also asked at
LUberSalesforce

Problem

Task

RideNow wants to identify its highest-efficiency drivers based on earnings per hour in the previous calendar month. Write a SQL query to return the drivers whose earnings per hour place them in the top 10% for that period.

Requirements

  1. Use only trips that were completed in the previous calendar month.
  2. For each driver, calculate:
    • total earnings as the sum of fare_amount
    • total hours worked as the sum of trip duration in hours
    • earnings per hour = total earnings / total hours worked
  3. Exclude drivers whose total worked hours are 0 or NULL.
  4. Return only drivers in the top 10% by earnings per hour using a ranking or percentile approach.
  5. Output driver_id, driver_name, total_earnings, total_hours, and earnings_per_hour, ordered by earnings_per_hour descending.

Table Definitions

drivers

columntypedescription
driver_idINTPrimary key for each driver
driver_nameVARCHAR(100)Driver full name
cityVARCHAR(50)Driver's primary city
active_statusVARCHAR(20)Current driver status

trips

columntypedescription
trip_idINTPrimary key for each trip
driver_idINTDriver assigned to the trip
trip_statusVARCHAR(20)Trip status such as completed or cancelled
started_atTIMESTAMPTrip start timestamp
ended_atTIMESTAMPTrip end timestamp
fare_amountDECIMAL(10,2)Fare earned for the trip

Sample Data

drivers

driver_iddriver_namecityactive_status
1Alice KimChicagoactive
2Ben OrtizChicagoactive
3Carla SinghBostonactive
4Diego ParkBostoninactive
5Elena RossiSeattleactive

trips

trip_iddriver_idtrip_statusstarted_atended_atfare_amount
1011completed2024-05-03 08:00:002024-05-03 09:00:0040.00
1021completed2024-05-10 10:00:002024-05-10 11:30:0075.00
1032completed2024-05-06 09:00:002024-05-06 11:00:0070.00
1043completed2024-05-08 14:00:002024-05-08 15:00:0065.00
1054cancelled2024-05-09 12:00:002024-05-09 12:30:000.00

Expected Output

driver_iddriver_nametotal_earningstotal_hoursearnings_per_hour
3Carla Singh165.002.0082.50

Schema

drivers
ColumnTypeDescription
driver_idPKINTUnique identifier for each driver
driver_nameVARCHAR(100)Driver full name
cityVARCHAR(50)Driver's primary city
active_statusVARCHAR(20)Current status of the driver
trips
ColumnTypeDescription
trip_idPKINTUnique identifier for each trip
driver_idINTDriver assigned to the trip
trip_statusVARCHAR(20)Trip completion status
started_atTIMESTAMPTrip start timestamp
ended_atTIMESTAMPTrip end timestamp
fare_amountDECIMAL(10,2)Fare earned from the trip

Sample Data

Example 1
Input{"trips":[["101","1","completed","2024-05-03 08:00:00","2024-05-03 09:00:00","40.00"],["102","1","completed","2024-05-10 10:00:00","2024-05-10 11:30:00","75.00"],["103","2","completed","2024-05-06 09:00:00","2024-05-06 11:00:00","70.00"],["104","2","completed","2024-05-20 13:00:00","2024-05-20 14:00:00","25.00"],["105","3","completed","2024-05-08 14:00:00","2024-05-08 15:00:00","65.00"],["106","3","completed","2024-05-18 18:00:00","2024-05-18 19:00:00","100.00"],["107","4","cancelled","2024-05-0Output[]

Problem

Task

RideNow wants to identify its highest-efficiency drivers based on earnings per hour in the previous calendar month. Write a SQL query to return the drivers whose earnings per hour place them in the top 10% for that period.

Requirements

  1. Use only trips that were completed in the previous calendar month.
  2. For each driver, calculate:
    • total earnings as the sum of fare_amount
    • total hours worked as the sum of trip duration in hours
    • earnings per hour = total earnings / total hours worked
  3. Exclude drivers whose total worked hours are 0 or NULL.
  4. Return only drivers in the top 10% by earnings per hour using a ranking or percentile approach.
  5. Output driver_id, driver_name, total_earnings, total_hours, and earnings_per_hour, ordered by earnings_per_hour descending.

Table Definitions

drivers

columntypedescription
driver_idINTPrimary key for each driver
driver_nameVARCHAR(100)Driver full name
cityVARCHAR(50)Driver's primary city
active_statusVARCHAR(20)Current driver status

trips

columntypedescription
trip_idINTPrimary key for each trip
driver_idINTDriver assigned to the trip
trip_statusVARCHAR(20)Trip status such as completed or cancelled
started_atTIMESTAMPTrip start timestamp
ended_atTIMESTAMPTrip end timestamp
fare_amountDECIMAL(10,2)Fare earned for the trip

Sample Data

drivers

driver_iddriver_namecityactive_status
1Alice KimChicagoactive
2Ben OrtizChicagoactive
3Carla SinghBostonactive
4Diego ParkBostoninactive
5Elena RossiSeattleactive

trips

trip_iddriver_idtrip_statusstarted_atended_atfare_amount
1011completed2024-05-03 08:00:002024-05-03 09:00:0040.00
1021completed2024-05-10 10:00:002024-05-10 11:30:0075.00
1032completed2024-05-06 09:00:002024-05-06 11:00:0070.00
1043completed2024-05-08 14:00:002024-05-08 15:00:0065.00
1054cancelled2024-05-09 12:00:002024-05-09 12:30:000.00

Expected Output

driver_iddriver_nametotal_earningstotal_hoursearnings_per_hour
3Carla Singh165.002.0082.50

Schema

drivers
ColumnTypeDescription
driver_idPKINTUnique identifier for each driver
driver_nameVARCHAR(100)Driver full name
cityVARCHAR(50)Driver's primary city
active_statusVARCHAR(20)Current status of the driver
trips
ColumnTypeDescription
trip_idPKINTUnique identifier for each trip
driver_idINTDriver assigned to the trip
trip_statusVARCHAR(20)Trip completion status
started_atTIMESTAMPTrip start timestamp
ended_atTIMESTAMPTrip end timestamp
fare_amountDECIMAL(10,2)Fare earned from the trip

Sample Data

Example 1
Input{"trips":[["101","1","completed","2024-05-03 08:00:00","2024-05-03 09:00:00","40.00"],["102","1","completed","2024-05-10 10:00:00","2024-05-10 11:30:00","75.00"],["103","2","completed","2024-05-06 09:00:00","2024-05-06 11:00:00","70.00"],["104","2","completed","2024-05-20 13:00:00","2024-05-20 14:00:00","25.00"],["105","3","completed","2024-05-08 14:00:00","2024-05-08 15:00:00","65.00"],["106","3","completed","2024-05-18 18:00:00","2024-05-18 19:00:00","100.00"],["107","4","cancelled","2024-05-0Output[]
Practice SQL
PostgreSQL
Open on desktop for the full SQL editor with syntax highlighting and autocomplete.
Up next
LCompare Driver Hourly Earnings by ZoneEasyUberAverage ETA and Outlier TripsEasyUberTop City-Pair Revenue by WeekdayMedium
Next question