Immediate Food Delivery III - Problem
Immediate Food Delivery III

You're working as a data analyst for a popular food delivery platform! ๐Ÿ•๐Ÿ“Š

The company wants to understand their delivery patterns by analyzing what percentage of orders are immediate versus scheduled on each day.

Problem Setup:
You have a Delivery table with the following structure:

Column NameType
delivery_idint
customer_idint
order_datedate
customer_pref_delivery_datedate

Key Definitions:
โ€ข Immediate Order: When customer_pref_delivery_date = order_date
โ€ข Scheduled Order: When customer_pref_delivery_date > order_date

Your Task:
Calculate the percentage of immediate orders for each unique order_date, rounded to 2 decimal places. Return results ordered by order_date ascending.

Expected Output Format:
order_date | immediate_percentage

Input & Output

example_1.sql โ€” Basic Example
$ 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: | order_date | immediate_percentage | |------------|---------------------| | 2019-08-01 | 0.00 | | 2019-08-02 | 100.00 | | 2019-08-09 | 100.00 | | 2019-08-11 | 0.00 | | 2019-08-21 | 0.00 | | 2019-08-24 | 100.00 |
๐Ÿ’ก Note: For 2019-08-01: 0 immediate out of 1 total = 0%. For 2019-08-02: 1 immediate out of 1 total = 100%. For 2019-08-09: 1 immediate out of 1 total = 100%. For 2019-08-11: 0 immediate out of 2 total = 0%. For 2019-08-21: 0 immediate out of 1 total = 0%. For 2019-08-24: 1 immediate out of 1 total = 100%.
example_2.sql โ€” Mixed Percentages
$ Input: Delivery table: | delivery_id | customer_id | order_date | customer_pref_delivery_date | |-------------|-------------|------------|----------------------------| | 1 | 1 | 2020-01-01 | 2020-01-01 | | 2 | 2 | 2020-01-01 | 2020-01-02 | | 3 | 3 | 2020-01-01 | 2020-01-01 | | 4 | 4 | 2020-01-02 | 2020-01-03 | | 5 | 5 | 2020-01-02 | 2020-01-02 | | 6 | 6 | 2020-01-02 | 2020-01-04 |
โ€บ Output: | order_date | immediate_percentage | |------------|---------------------| | 2020-01-01 | 66.67 | | 2020-01-02 | 33.33 |
๐Ÿ’ก Note: For 2020-01-01: 2 immediate orders (delivery_id 1,3) out of 3 total = 66.67%. For 2020-01-02: 1 immediate order (delivery_id 5) out of 3 total = 33.33%.
example_3.sql โ€” Edge Case: All Scheduled
$ Input: Delivery table: | delivery_id | customer_id | order_date | customer_pref_delivery_date | |-------------|-------------|------------|----------------------------| | 1 | 1 | 2023-12-25 | 2023-12-26 | | 2 | 2 | 2023-12-25 | 2023-12-27 | | 3 | 3 | 2023-12-26 | 2023-12-26 |
โ€บ Output: | order_date | immediate_percentage | |------------|---------------------| | 2023-12-25 | 0.00 | | 2023-12-26 | 100.00 |
๐Ÿ’ก Note: For 2023-12-25: 0 immediate out of 2 total = 0.00% (both orders scheduled for later). For 2023-12-26: 1 immediate out of 1 total = 100.00%.

Visualization

Tap to expand
๐Ÿ• Food Delivery Analytics Dashboard๐Ÿ“… Daily OrdersImmediateScheduledImmediateImmediateGROUP BY date๐Ÿ“Š Grouped DataJan 1: 3 immediate, 1 scheduledJan 2: 2 immediate, 2 scheduledJan 3: 1 immediate, 3 scheduledCalculate %๐Ÿ“ˆ ResultsJan 1: 75.00%Jan 2: 50.00%Jan 3: 25.00%๐Ÿ”ง SQL Formula BreakdownSUM(CASE WHEN customer_pref_delivery_date = order_date THEN 1 ELSE 0 END) * 100.0 / COUNT(*)โ€ข CASE WHEN: Conditionally count immediate orders (1 or 0)โ€ข SUM: Total up all the 1s (immediate order count)โ€ข COUNT(*): Total orders in each groupโ€ข * 100.0: Convert to percentageโšก Performance BenefitsSingle table scan O(n) vs Multiple scans O(nยฒ)Database-optimized GROUP BY with efficient aggregation
Understanding the Visualization
1
Data Collection
Orders flow in throughout each day with delivery preferences
2
Daily Grouping
Group all orders by their order date using GROUP BY
3
Conditional Counting
Use CASE statements to count immediate vs total orders simultaneously
4
Percentage Calculation
Calculate and round percentages for each day
5
Report Generation
Sort by date and generate the final analytics report
Key Takeaway
๐ŸŽฏ Key Insight: GROUP BY with conditional aggregation (CASE WHEN) allows us to count different types of records and calculate percentages in a single, efficient database operation.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through all n records, with GROUP BY typically O(n) with hash-based grouping

n
2n
โœ“ Linear Growth
Space Complexity
O(d)

Space for d unique dates in GROUP BY result and hash table

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค delivery_id โ‰ค 105
  • 1 โ‰ค customer_id โ‰ค 104
  • order_date and customer_pref_delivery_date are valid dates
  • customer_pref_delivery_date โ‰ฅ order_date (preferred delivery cannot be before order date)
  • All delivery_id values are unique
  • Result percentages must be rounded to exactly 2 decimal places
Asked in
DoorDash 85 Uber 72 Amazon 45 Grubhub 38
94.5K Views
High Frequency
~15 min Avg. Time
2.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen