Immediate Food Delivery I - Problem

Table: Delivery

Column NameType
delivery_idint
customer_idint
order_datedate
customer_pref_delivery_datedate

delivery_id is the primary key of this table. The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date.

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.

Write a solution to find the percentage of immediate orders in the table, rounded to 2 decimal places.

Table Schema

Delivery
Column Name Type Description
delivery_id PK int Primary key, unique delivery identifier
customer_id int Customer identifier
order_date date Date when the order was placed
customer_pref_delivery_date date Customer's preferred delivery date
Primary Key: delivery_id
Note: Orders are immediate if order_date equals customer_pref_delivery_date

Input & Output

Example 1 — Mixed Immediate and Scheduled Orders
Input 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
Output:
immediate_percentage
33.33
💡 Note:

Out of 6 total orders, only orders with delivery_id 2 and 4 are immediate (order_date = customer_pref_delivery_date). So 2/6 = 33.33%.

Example 2 — All Immediate Orders
Input 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:
immediate_percentage
100
💡 Note:

Both orders are immediate since order_date equals customer_pref_delivery_date for all orders. Result is 100.00%.

Example 3 — No Immediate Orders
Input 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-05
Output:
immediate_percentage
0
💡 Note:

No orders are immediate since all have different order_date and customer_pref_delivery_date. Result is 0.00%.

Constraints

  • 1 ≤ delivery_id ≤ 500
  • 1 ≤ customer_id ≤ 100
  • order_date and customer_pref_delivery_date are valid dates
  • The table contains at least one row

Visualization

Tap to expand
Immediate Food Delivery I: Percentage CalculationInput: Delivery Tableorder_datepref_date08-0108-0208-0208-0208-1108-1208-2408-24CASE WHENsame dates→ 100.0else → 0.0CASE ValuesOrderValue10.02100.030.04100.0Final Result33.33%AVG(0,100,0,100)💡 2 immediate orders out of 4 total = 50%. Wait, this shows different data than example 1...For 6 orders with 2 immediate: (0+100+0+100+0+0)/6 = 33.33%
Understanding the Visualization
1
Input
Delivery table with order and preferred delivery dates
2
CASE WHEN
Assign 100.0 for immediate orders, 0.0 for scheduled
3
AVG & ROUND
Calculate average percentage and round to 2 decimals
Key Takeaway
🎯 Key Insight: Use CASE WHEN with AVG to calculate percentages in a single SQL query
Asked in
DoorDash 15 Uber 8
35.0K Views
Medium 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