KatInterview

Multi-Part SQL Challenge on a Food Delivery Schema

SQL & DataModel answer
MediumModel answerSQL & Datawindow-functionsaggregationjoins

Last updated

Write six targeted SQL queries spanning window functions, event anomalies, time-based filtering, and promo uplift analysis on a realistic food delivery dataset.

Setting the Scene

You are working with a food delivery platform's analytics database (BigQuery dialect). All timestamps are UTC. Set today = 2025-09-01 for any relative date calculations.

Use the schema and sample data below to write precise SQL for tasks (a)–(f). Before each query, state any assumptions you are making about definitions, null handling, or time boundaries.


Schema & Sample Data

customers

customer_idsignup_datecity
12025-07-10SF
22025-08-01SF
32025-08-20NYC

merchants

merchant_idnamecitycuisine
10SushiCoSFJapanese
11TacoTownSFMexican
12PizzaBoxNYCItalian

dashers

dasher_idsignup_datevehicle_type
1002025-06-01car
1012025-08-15bike

promotions

promo_idpromo_typediscount_amountstart_dateend_date
900pct1010.002025-08-012025-08-31
901flat55.002025-08-102025-09-10

orders

order_idcustomer_idmerchant_iddasher_idcreated_ataccepted_atpicked_up_atdelivered_atcanceled_atsubtotaldelivery_feetip_amountpromo_idstatus
50011101002025-08-28 12:002025-08-28 12:022025-08-28 12:202025-08-28 12:35null30.003.995.00900delivered
5002211null2025-08-29 18:05nullnullnull2025-08-29 18:1015.002.990.00nullcanceled
50031111012025-08-31 19:002025-08-31 19:042025-08-31 19:20nullnull20.002.993.00901in_progress
50043121002025-08-24 11:302025-08-24 11:31null2025-08-24 12:10null22.004.990.00nulldelivered

order_events

order_idevent_timeevent_type
50012025-08-28 12:00created
50012025-08-28 12:02accepted
50012025-08-28 12:20picked_up
50012025-08-28 12:35delivered
50022025-08-29 18:05created
50022025-08-29 18:10canceled
50032025-08-31 19:00created
50032025-08-31 19:04accepted
50042025-08-24 11:30created
50042025-08-24 11:31accepted
50042025-08-24 12:10delivered

Tasks

(a) Compute the daily on-time delivery rate broken out by city for the period 2025-08-26 through 2025-09-01 (inclusive). Define on-time as delivered_at <= created_at + 45 minutes, applied only to non-canceled orders. A NULL delivered_at counts as late. Return city, date, on_time_deliveries, total_eligible, on_time_rate.

(b) For August 2025, rank the top 3 merchants per city by GMV, where GMV = subtotal + delivery_fee + tip_amount − COALESCE(discount_amount, 0). Exclude canceled orders from GMV but keep them for the cancellation tie-breaker. Break ties by fewest cancellations, then by lower merchant_id. Return city, merchant_id, rank_in_city, gmv, cancellations.

(c) Using order_events, identify orders that have a delivered event but no picked_up event that occurs strictly before it. Return order_id and a condensed event timeline up to and including the first delivery, as evidence of the anomaly.

(d) For each dasher and ISO calendar week, compute the acceptance rate: the share of dasher-assigned orders where accepted_at falls within 3 minutes of created_at. Exclude orders that were canceled before they were accepted. Return dasher_id, week_start_date, assigned_orders, accepted_within_3m, acceptance_rate.

(e) Find customers whose entire order history consists of exactly two orders, both of which were canceled. Return customer_id along with the two canceled_at timestamps in ascending order.

(f) For each merchant in August 2025 (non-canceled orders only), estimate the average order value (AOV) for promo orders versus non-promo orders. Return merchant_id, promo_orders, non_promo_orders, aov_promo, aov_nonpromo, diff. Then briefly describe how you would construct a 95% confidence interval for diff using either a SQL-based approximation or a short Python snippet.

Hints

Hint 1

For (a), use a half-open timestamp range (>= 2025-08-26, < 2025-09-02) so the full day of Sep 1 is included, and let COUNTIF handle the on-time boolean — NULL delivered_at naturally fails the <= condition, so it counts as late without extra logic.

Hint 2

For (b), put the cancellation count inside the CTE alongside GMV rather than filtering canceled rows out in the WHERE clause — you need them to compute the tie-breaker. Then apply ROW_NUMBER with the three-part ORDER BY (gmv DESC, cancellations ASC, merchant_id ASC) so ranks are always unique, and use QUALIFY to keep only the top 3.

Hint 3

For (c), aggregate order_events once to get the earliest delivered and earliest picked_up timestamps per order, then flag rows where first_pickup IS NULL or first_pickup >= first_delivered — both are anomalies because 'strictly earlier' is required. Build the timeline with STRING_AGG ordered by event_time, capped at the first delivery.