Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Total Driver Time Per Day

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

Your question is Total Driver Time Per Day. 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

Uber's driver activity reporting needs daily totals even when an Uber trip starts on one calendar day and ends on another. Trip timestamps are stored in UTC. Write a PostgreSQL query that calculates driver time for each day from 2024-01-01 through 2024-01-04, inclusive.

Requirements

  1. Split every valid trip at midnight boundaries, so each calendar day receives only the time occurring on that day.
  2. Clip trips to the reporting period and exclude trips with missing timestamps, nonpositive duration, no matching driver, or no overlap with the period.
  3. Return one row per driver and active date, with total time in hours rounded to two decimal places. Sort by driver name and activity date.

Schema

drivers
ColumnTypeDescription
driver_idPKINTEGERUnique Uber driver identifier
driver_nameVARCHAR(100)Driver display name
trips
ColumnTypeDescription
trip_idPKINTEGERUnique trip identifier
driver_idINTEGERAssigned Uber driver identifier
started_atTIMESTAMPTrip start timestamp in UTC
ended_atTIMESTAMPTrip end timestamp in UTC
Tablesdriverstrips
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results