Find the least efficient driver between '2024-01-01' (inclusive) and '2024-03-02' (exclusive). A driver's efficiency is deliveries_completed / total_hours_driven for that period, and the least efficient driver has the lowest ratio. Completed deliveries have status = 'completed'. Return driver_id, driver_name, deliveries_completed, total_hours_driven and efficiency_ratio for that driver.
Input Table(s)
drivers has one row per driver.
| Column | Type | Meaning |
|---|
| driver_id | INT | Driver identifier (primary key) |
| driver_name | VARCHAR | Driver's name |
deliveries has one row per delivery assigned to a driver.
| Column | Type | Meaning |
|---|
| delivery_id | INT | Delivery identifier (primary key) |
| driver_id | INT | Driver assigned to the delivery (references drivers) |
| delivery_date | DATE | Date the delivery belongs to |
| status | VARCHAR | One of 'completed', 'failed', 'pending' |
driver_logs has one row per logged driving shift.
| Column | Type | Meaning |
|---|
| log_id | INT | Log identifier (primary key) |
| driver_id | INT | Driver who logged the hours (references drivers) |
| log_date | DATE | Date of the shift |
| hours_driven | DECIMAL(5,2) | Hours driven in the shift, always positive |
Output Contract
-
Columns in this order:
driver_id
,
driver_name
,
deliveries_completed
,
total_hours_driven
,
efficiency_ratio
.
-
deliveries_completed
is the number of the driver's deliveries with
status = 'completed'
and a
delivery_date
in the period (0 when there are none).
-
total_hours_driven
is the sum of
hours_driven
over the driver's logs with a
log_date
in the period, rounded to 2 decimals.
-
efficiency_ratio
is
deliveries_completed
divided by the unrounded hour total, rounded to 4 decimals.
-
Return exactly one row: the driver with the lowest
efficiency_ratio
(compared after rounding to 4 decimals). Ties on ratio go to the smallest
driver_id
.
Example
Input drivers:
| driver_id | driver_name |
|---|
| 1 | Ava |
| 2 | Ben |
| 3 | Cara |
| 4 | Dev |
Input driver_logs:
| log_id | driver_id | log_date | hours_driven |
|---|
| 1 | 1 | 2024-01-05 | 4.50 |
| 2 | 1 | 2024-02-10 | 5.50 |
| 3 | 2 | 2024-01-20 | 5.00 |
| 4 | 3 | 2024-03-01 | 2.25 |
| 5 | 3 | 2024-03-02 | 6.00 |
| 6 | 4 | 2023-12-31 | 8.00 |
Input deliveries:
| delivery_id | driver_id | delivery_date | status |
|---|
| 101 | 1 | 2024-01-05 | completed |
| 102 | 1 | 2024-02-10 | completed |
| 103 | 1 | 2024-02-10 | failed |
| 104 | 2 | 2024-01-20 | completed |
| 105 | 3 | 2024-03-01 | pending |
| 106 | 3 | 2024-02-15 | failed |
| 107 | 4 | 2024-02-01 | completed |
Expected output:
| driver_id | driver_name | deliveries_completed | total_hours_driven | efficiency_ratio |
|---|
| 3 | Cara | 0 | 2.25 | 0.0000 |
Ava has 2 completed deliveries in 10.00 hours and Ben has 1 in 5.00 hours, so both sit at 0.2000. Cara's only log in the period is the 2.25-hour shift on 2024-03-01 (the 2024-03-02 shift falls on the excluded end date), and she has no completed deliveries, so her ratio is 0. Dev completed a delivery but logged no hours in the period, so he is not considered.
Constraints and Clarifications
-
The same period applies to both tables: dates on or after 2024-01-01 and before 2024-03-02. March 1 is included and March 2 is not.
-
Only drivers with logged hours greater than zero in the period are considered. Because
hours_driven
is always positive, that means at least one log in the period. At least one driver qualifies.
-
A driver with logged hours and no completed deliveries is a valid candidate with a ratio of 0.
-
Only
'completed'
deliveries count.
'failed'
and
'pending'
deliveries are ignored.
-
A driver can have several logs and several deliveries on the same date.
-
No column contains NULL.
-
drivers
has at most 10,000 rows;
deliveries
and
driver_logs
have at most 100,000 rows each.