Prepin
Log in

[Considering a dataset that contains information about a food delivery service] Construct a SQL query to determine the driver with the lowest efficiency, measured by deliveries completed relative to the time spent driving, over the past two months.

Approach / Explanation

In this question, you are required to create a SQL query that identifies the driver with the lowest efficiency in terms of deliveries made per hour spent driving over the past two months, using a dataset from a food delivery service. To effectively tackle this question, you need to grasp the dataset's structure, the information it holds, and the specific data fields necessary to calculate deliveries per hour driven. Key aspects to consider in addressing this question: Driver information : Details identifying the driver. Delivery statistics : The total number of deliveries completed by each driver in the last two months. Time statistics : The total amount of time each driver has been on the road in the last two months. Your query should carry out the following tasks: Filter the data to focus on the last two months. Calculate each driver's efficiency by dividing the total number of deliveries by the total driving time. Sort the drivers in ascending order based on their efficiency. Select the driver with the lowest efficiency. Ensure that your SQL query is straightforward, easy to read, and includes comments where appropriate to clarify your code.

Suggested Answer

Consider a database called deliveryDB that has a table named driver_data . This table includes the columns driver_id (int), delivery_count (int), driving_time (float), and delivery_date (date). The delivery_count indicates how many deliveries the driver has completed, driving_time reflects the total hours spent driving, and delivery_date denotes the date of each delivery. The following SQL query filters the data for the past two months, calculates the efficiency of each driver, and identifies the least efficient driver: SELECT driver_id, SUM(delivery_count)/SUM(driving_time) AS efficiency FROM driver_data WHERE delivery_date >= CURDATE() - INTERVAL 2 MONTH GROUP BY driver_id ORDER BY efficiency ASC LIMIT 1; This query employs SUM to compute the overall delivery_count and driving_time for each driver over the past two months. The GROUP BY clause aggregates these totals by driver_id . Efficiency is determined by dividing the total delivery_count by the total driving_time . Finally, the ORDER BY and LIMIT clauses are utilized to select the driver with the lowest efficiency.

Alternative Answer

If we have a time column recorded in hours, here’s another way to formulate the query with the same logic: SELECT TOP 1 driver_id, (SUM(delivery_count)/SUM(driving_time)) AS efficiency FROM driver_data WHERE delivery_date BETWEEN DATEADD(month, -2, GETDATE()) AND GETDATE() GROUP BY driver_id ORDER BY efficiency; This query utilizes the DATEADD and GETDATE functions to establish the date range for the past two months. The TOP keyword is employed to identify the least efficient driver, replacing the LIMIT clause. The overall logic remains consistent with the previous example.

Question Details

Difficulty & Category

Medium
technical

Companies

Instacart

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.