Compute the percentage of immediate deliveries among customers' first orders
Company: Dropbox
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A food delivery service stores one row per order in the `Delivery` table. An order is immediate when `customer_pref_delivery_date` equals `order_date`; otherwise it is scheduled. A customer's first order is the one with the earliest `order_date`. Write a query that returns `immediate_percentage`: the percentage of immediate orders among all customers' first orders, rounded to 2 decimal places.
```hint Shrink the table first
Reduce `Delivery` to exactly one row per customer before you compute any percentage.
```
### Input Table(s)
Table `Delivery`
| Column | Type | Meaning |
|---|---|---|
| delivery_id | INT | Id of the order |
| customer_id | INT | Customer who placed the order (NOT NULL) |
| order_date | DATE | Date the order was placed (NOT NULL) |
| customer_pref_delivery_date | DATE | Delivery date the customer asked for (NOT NULL) |
Primary key: `delivery_id`.
### Output Contract
- Exactly one row with one column, `immediate_percentage`.
- The value is 100 times the number of customers whose first order is immediate divided by the number of customers, rounded to 2 decimal places (for example 33.33, or 50.00 when exactly half the first orders are immediate).
- The result is a single row, so no ordering is needed.
### Example
Input `Delivery`:
| delivery_id | customer_id | order_date | customer_pref_delivery_date |
|---|---|---|---|
| 1 | 201 | 2024-05-01 | 2024-05-01 |
| 2 | 202 | 2024-05-02 | 2024-05-04 |
| 3 | 201 | 2024-05-03 | 2024-05-05 |
| 4 | 203 | 2024-05-03 | 2024-05-03 |
| 5 | 202 | 2024-05-06 | 2024-05-06 |
| 6 | 204 | 2024-05-07 | 2024-05-08 |
| 7 | 205 | 2024-05-12 | 2024-05-12 |
| 8 | 205 | 2024-05-08 | 2024-05-10 |
| 9 | 206 | 2024-05-09 | 2024-05-11 |
Expected output:
| immediate_percentage |
|---|
| 33.33 |
The first orders are rows 1, 2, 4, 6, 8 and 9, one for each of the 6 customers. Customer 205's first order is row 8, because 2024-05-08 is earlier than 2024-05-12, even though row 7 has the smaller `delivery_id`. Rows 1 and 4 are the only immediate first orders, so the answer is 2 out of 6, which is 33.333... percent and rounds to 33.33. Rows 5 and 7 are immediate but are not first orders, so they do not count.
### Constraints and Clarifications
- `Delivery` has between 1 and 10,000 rows, so there is always at least one customer.
- No column is NULL.
- `delivery_id` is unique, so each row is one order.
- Each customer has exactly one order on their earliest `order_date`, so every customer has exactly one first order.
- `customer_pref_delivery_date` is never earlier than `order_date`.
- Only first orders count. A customer's later orders are ignored, even when they are immediate.
- When the exact percentage ends in 5 at the third decimal place, round away from zero, which is what PostgreSQL `ROUND` does on a `numeric` value.
Overview: Write a SQL query that finds each customer's first order in a food delivery table and returns the percentage of those first orders whose preferred delivery date equals the order date, rounded to two decimals. It tests per-group minimums, filtering to one row per customer, and numeric division in PostgreSQL.