Compute the percentage of immediate food delivery orders
Company: Databricks
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
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. Write a query that returns the percentage of immediate orders in `Delivery`, rounded to 2 decimal places, as `immediate_percentage`.
```hint Watch the division
In PostgreSQL, dividing one integer count by another truncates the result. Check the type of each operand before you round.
```
### 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 immediate orders divided by the total number of orders, rounded to 2 decimal places (for example 42.86, or 50.00 when exactly half the 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 | 101 | 2024-03-01 | 2024-03-01 |
| 2 | 102 | 2024-03-01 | 2024-03-03 |
| 3 | 101 | 2024-03-04 | 2024-03-05 |
| 4 | 103 | 2024-03-04 | 2024-03-04 |
| 5 | 104 | 2024-03-06 | 2024-03-09 |
| 6 | 102 | 2024-03-07 | 2024-03-07 |
| 7 | 105 | 2024-03-08 | 2024-03-10 |
Expected output:
| immediate_percentage |
|---|
| 42.86 |
Orders 1, 4 and 6 have matching dates, so 3 of the 7 orders are immediate. 3 divided by 7 is 0.428571..., which is 42.857... percent and rounds to 42.86.
### Constraints and Clarifications
- `Delivery` has between 1 and 10,000 rows, so the total is never zero.
- No column is NULL, so every order is either immediate or scheduled.
- `delivery_id` is unique, so each row is one order. A customer can appear on many rows, and every one of those orders counts.
- Any difference between the two dates makes the order scheduled.
- 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 over a food delivery orders table that returns the share of orders whose preferred delivery date matches the order date, as a percentage rounded to two decimals. It tests conditional aggregation, numeric types in division, and rounding in PostgreSQL.
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. Write a query that returns the percentage of immediate orders in `delivery`, rounded to 2 decimal places, as `immediate_percentage`.
Table `delivery`:
- `delivery_id` INTEGER, primary key: id of the order
- `customer_id` INTEGER NOT NULL: customer who placed the order
- `order_date` DATE NOT NULL: date the order was placed
- `customer_pref_delivery_date` DATE NOT NULL: delivery date the customer asked for
Output: exactly one row with one column, `immediate_percentage`. The value is 100 times the number of immediate orders divided by the total number of orders, rounded to 2 decimal places (for example 42.86 when 3 of 7 orders are immediate, or 50.00 when exactly half the orders are immediate). The result is a single row, so no ordering is needed.
Constraints and clarifications:
- `delivery` has between 1 and 10,000 rows, so the total is never zero.
- No column is NULL, so every order is either immediate or scheduled.
- `delivery_id` is unique, so each row is one order. A customer can appear on many rows, and every one of those orders counts.
- Any difference between the two dates makes the order scheduled.
- 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.
Tables
delivery(delivery_id INTEGER, customer_id INTEGER, order_date DATE, customer_pref_delivery_date DATE)
Hints
- In PostgreSQL, dividing one integer count by another truncates the result. Check the type of each operand before you round.
- Each row is one order, so a customer who placed several orders contributes every one of them to both counts.