Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top Driver Earnings Per Hour

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Top Driver Earnings Per Hour. Start with the requirements and the two tables on the right.

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.

Problem

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.

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
Tablesdriverstrips
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results