Immediate Food Delivery I - Problem
๐ Food Delivery Analysis: Immediate vs Scheduled Orders
You're working as a data analyst for a popular food delivery platform! The company wants to understand customer ordering behavior by analyzing their delivery preferences.
You have access to the Delivery table with the following structure:
| Column Name | Type |
|---|---|
| delivery_id | int |
| customer_id | int |
| order_date | date |
| customer_pref_delivery_date | date |
Business Rules:
- ๐ฆ Immediate Order: When
customer_pref_delivery_dateequalsorder_date - ๐
Scheduled Order: When
customer_pref_delivery_dateis afterorder_date
Your mission: Calculate the percentage of immediate orders in the table, rounded to 2 decimal places. This metric helps the business understand how many customers want their food delivered immediately versus planning ahead!
Input & Output
basic_example.sql โ Standard Case
$
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 |
+-------------+-------------+------------+-----------------------------+
โบ
Output:
50.00
๐ก Note:
Out of 4 total orders, 2 are immediate (delivery_id 2 and 4 have matching dates). Percentage = (2/4) ร 100 = 50.00%
all_immediate.sql โ All Immediate Orders
$
Input:
Delivery table:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1 | 1 | 2019-08-01 | 2019-08-01 |
| 2 | 2 | 2019-08-02 | 2019-08-02 |
+-------------+-------------+------------+-----------------------------+
โบ
Output:
100.00
๐ก Note:
All 2 orders are immediate (both have matching order and preferred delivery dates). Percentage = (2/2) ร 100 = 100.00%
no_immediate.sql โ No Immediate Orders
$
Input:
Delivery table:
+-------------+-------------+------------+-----------------------------+
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
+-------------+-------------+------------+-----------------------------+
| 1 | 1 | 2019-08-01 | 2019-08-03 |
| 2 | 2 | 2019-08-02 | 2019-08-05 |
+-------------+-------------+------------+-----------------------------+
โบ
Output:
0.00
๐ก Note:
No orders are immediate (all preferred delivery dates are after order dates). Percentage = (0/2) ร 100 = 0.00%
Visualization
Tap to expand
Understanding the Visualization
1
Order Intake
Each order comes in with order date and preferred delivery date
2
Smart Classification
System automatically compares dates: same date = immediate, different date = scheduled
3
Real-time Counting
Dashboard maintains running count of immediate vs total orders
4
Percentage Display
Business metric updates live: (immediate_count รท total_count) ร 100
Key Takeaway
๐ฏ Key Insight: Use conditional aggregation in SQL to count specific cases and calculate percentages in a single, efficient query that scales well with large datasets.
Time & Space Complexity
Time Complexity
O(n)
Single scan through all delivery records
โ Linear Growth
Space Complexity
O(1)
Only storing aggregate counters
โ Linear Space
Constraints
- 1 โค Number of delivery records โค 500
- delivery_id is the primary key
- customer_pref_delivery_date is on the same day or after order_date
- Result must be rounded to exactly 2 decimal places
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code