Measure Immediate Deliveries among Customers’ First Orders
Company: Google
Role: Software Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
### Problem
Return immediate_percentage: the percentage of customers whose first order was immediate, rounded to two decimal places. An order is immediate exactly when order_date equals customer_pref_delivery_date. First means earliest order_date; the source guarantees exactly one first order for each customer.
### Tables
Table: `Delivery`
```text
+-----------------------------+---------+
| Column Name | Type |
+-----------------------------+---------+
| delivery_id | int |
| customer_id | int |
| order_date | date |
| customer_pref_delivery_date | date |
+-----------------------------+---------+
delivery_id is the column of unique values of this table.
The table holds information about food delivery to customers that make orders at some date and specify a preferred delivery date (on the same order date or after it).
```
### Constraints
- In this version, `Delivery` contains at least one order, so at least one customer is represented.
### Example
```text
Delivery (delivery_id,customer_id,order_date,customer_pref_delivery_date): (1,1,2021-01-01,2021-01-01), (2,1,2021-01-02,2021-01-04), (3,2,2021-01-01,2021-01-03), (4,2,2021-01-02,2021-01-02)
Output (immediate_percentage): (50.00)
```
### What a Strong Answer Covers
- Use only the earliest order for each customer.
- Classify first orders by their actual and preferred dates, then report the rounded percentage.
### Follow-up Questions
- How would you report the metric separately for each first-order month?
- How would you define the first order if several orders could share the earliest date?
Overview: Find the percentage of customers whose earliest order date matches their preferred delivery date.