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 NameType
delivery_idint
customer_idint
order_datedate
customer_pref_delivery_datedate

Business Rules:

  • ๐Ÿ“ฆ Immediate Order: When customer_pref_delivery_date equals order_date
  • ๐Ÿ“… Scheduled Order: When customer_pref_delivery_date is after order_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
๐Ÿ• Food Delivery Analytics Dashboard๐Ÿ“ฆ Immediate Orders33%Same day delivery requested๐Ÿ“… Scheduled Orders67%Future delivery requested๐ŸŽฏ Business InsightSQL Query: COUNT(CASE WHEN order_date = pref_date THEN 1 END) / COUNT(*) * 100Result: 33.33% of customers want immediate delivery๐Ÿ’ก This helps optimize kitchen staffing and delivery fleet scheduling
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

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

Only storing aggregate counters

n
2n
โœ“ 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
Asked in
DoorDash 45 Uber 38 Amazon 32 Grubhub 28
28.4K Views
High Frequency
~8 min Avg. Time
892 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