Interview Questions/SQL/Immediate Food Delivery II

Immediate Food Delivery II

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Description

You have a Delivery table. Each delivery has an order_date and a customer_pref_delivery_date. A delivery is considered 'immediate' if the order_date equals the customer_pref_delivery_date; otherwise it is 'scheduled'. Each customer's first order is the one with the minimum order_date. Write a SQL query to find the percentage of customers whose first order was immediate. Round to 2 decimal places.

Database Schema

Delivery

Column NameTypeDescription
delivery_idINTPrimary key
customer_idINTCustomer identifier
order_dateDATEDate order was placed
customer_pref_delivery_dateDATECustomer's preferred delivery date

Example

Delivery

delivery_idcustomer_idorder_datecustomer_pref_delivery_date
112019-08-012019-08-02
222019-08-022019-08-02
312019-08-112019-08-12
432019-08-242019-08-24
532019-08-212019-08-22
622019-08-112019-08-13
742019-08-092019-08-09

Output

immediate_percentage
50

Explanation:

Customer 1's first order (id=1) is scheduled. Customer 2's first order (id=2) is immediate. Customer 3's first order (id=5) is scheduled. Customer 4's first order (id=7) is immediate. 2 out of 4 = 50.00%.

Approach hint

Start with a simple approach, explain the trade-off, then move toward a cleaner or more scalable solution.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

SQL Editor
Loading...

Delivery

delivery_idcustomer_idorder_datecustomer_pref_delivery_date
112019-08-012019-08-02
222019-08-022019-08-02
312019-08-112019-08-12
432019-08-242019-08-24
532019-08-212019-08-22
622019-08-112019-08-13
742019-08-092019-08-09

Output

immediate_percentage
50