Completed trips by hour of day

dates

Completed trips by hour of day

Uber SQL Interview Question

Uber's marketplace team wants to know which hours of the day are busiest, so it can encourage more drivers to be online at those times.

For each hour of the day (0 to 23), count the completed trips that were requested in that hour. Show the hour as hour_of_day and the count as completed_trips, and leave out hours with no completed trips. Sort the rows by hour_of_day, earliest first.

Asked of

  • Data Analyst
  • Product Analyst
  • Data Scientist
  • Data Engineer
  • Analytics Engineer

tripsTable14 rows

Column NameType
trip_idBIGINT
rider_idBIGINT
driver_idBIGINT
cityVARCHAR
requested_atTIMESTAMP
statusVARCHAR
distance_kmDOUBLE
fareDOUBLE

tripsExample Input

trip_idrider_iddriver_idcityrequested_atstatusdistance_kmfare
9001301401Chicago2024-05-03 07:42:00completed8.418.9
9002302402Chicago2024-05-03 08:15:00completed3.19.5
9003303403Austin2024-05-03 08:47:00rider_canceled00
9004304404Austin2024-05-03 12:05:00completed12.624.3
9005305401Chicago2024-05-03 17:30:00completed5.213.4
9006306405Seattle2024-05-03 17:55:00driver_canceled00
9007307406Seattle2024-05-03 18:10:00completed9.826.1
9008308402Chicago2024-05-03 18:25:00completed4.411.2

Example Output

hour_of_daycompleted_trips
71
81
121
171
182

Explanation

Trips requested at 6:10 p.m. and 6:25 p.m. both fall in hour 18, so that hour has 2 completed trips. Trip 9003 was requested at 8:47 a.m., but the rider canceled it, so it does not add to hour 8. Every other hour in the example has 1 completed trip.

The example above is a small slice of the data. Your query runs against the full tables.