SQL
Lab
Uber Uber Interview Question 03

Driver
Utilization
Rate

Calculate utilization rate = total trip time / total available time per driver.

Table Schema

Inspect
Table

interactive
ColumnType
driver_idinteger
trip_durationinteger
available_durationinteger

Sample Data

Input
Output

Sample Input: driver_activity
driver_idtrip_durationavailable_duration
201300600
202200500
203400800
204100400
205350700
Expected Output
driver_idutilization_rate
2010.50
2020.40
2030.50
2040.25
2050.50

SQL Editor

Run
Query

postgresql
Waiting for query

driver_idutilization_rate
2010.50
2020.40
2030.50
2040.25
2050.50

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT driver_id, ROUND(1.0 * SUM(trip_duration) / SUM(available_duration), 2) AS utilization_rate
FROM driver_activity
GROUP BY driver_id;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving