Table: Delivery
+-----------------------------+---------+ | Column Name | Type | +-----------------------------+---------+ | delivery_id | int | | customer_id | int | | order_date | date | | customer_pref_delivery_date | date | +-----------------------------+---------+ delivery_id is the column of unique values of this table. The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).
If the customer's preferred delivery date is the same as the order date, then the order is called immediate; otherwise, it is called scheduled.
The first order of a customer is the order with the earliest order date that the customer made. It is guaranteed that a customer has precisely one first order.
Write a solution to find the percentage of immediate orders in the first orders of all customers, rounded to 2 decimal places.
The result format is in the following example.
Example 1:
Input: Delivery table: +-------------+-------------+------------+-----------------------------+ | delivery_id | customer_id | order_date | customer_pref_delivery_date | +-------------+-------------+------------+-----------------------------+ | 1 | 1 | 2019-08-01 | 2019-08-02 | | 2 | 2 | 2019-08-02 | 2019-08-02 | | 3 | 1 | 2019-08-11 | 2019-08-12 | | 4 | 3 | 2019-08-24 | 2019-08-24 | | 5 | 3 | 2019-08-21 | 2019-08-22 | | 6 | 2 | 2019-08-11 | 2019-08-13 | | 7 | 4 | 2019-08-09 | 2019-08-09 | +-------------+-------------+------------+-----------------------------+ Output: +----------------------+ | immediate_percentage | +----------------------+ | 50.00 | +----------------------+ Explanation: The customer id 1 has a first order with delivery id 1 and it is scheduled. The customer id 2 has a first order with delivery id 2 and it is immediate. The customer id 3 has a first order with delivery id 5 and it is scheduled. The customer id 4 has a first order with delivery id 7 and it is immediate. Hence, half the customers have immediate first orders.
In #1174 Immediate Food Delivery II, the goal is to determine the percentage of customers whose first order was delivered immediately. An order is considered immediate when order_date = customer_pref_delivery_date. The key challenge is correctly identifying each customer’s earliest order and then checking whether that order qualifies as immediate.
A common approach is to first determine the first order for every customer using aggregation such as MIN(order_date) grouped by customer_id, or by using a window function like ROW_NUMBER() ordered by order_date. After isolating these first orders, apply a conditional check to count how many satisfy the immediate delivery condition. Finally, compute the percentage using an aggregate function such as AVG() or a ratio of counts multiplied by 100.
This approach leverages SQL grouping and conditional aggregation, scanning the table once to derive the required statistics efficiently.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Group By + MIN(order_date) | O(n) | O(1) to O(n) depending on execution plan |
| Window Function (ROW_NUMBER) | O(n log n) due to sorting | O(n) |
Learn With Chirag
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, similar SQL aggregation and analytics problems are common in technical interviews, especially for data-related roles. Interviewers often test your ability to extract insights using grouping, filtering, and window functions.
The optimal approach is to first identify each customer's earliest order using MIN(order_date) or a window function like ROW_NUMBER(). Then check whether that order is an immediate delivery and compute the percentage using conditional aggregation.
Key SQL concepts include grouping, window functions, and conditional aggregation. Understanding how to identify the first record per group and calculate ratios using aggregate functions is essential.
The solution primarily uses relational table operations such as GROUP BY, filtering, and joins or window functions. These allow you to isolate the first order per customer and compute statistics across the dataset.