AnalystPath

Daily Failed-Delivery Rate

SQLHardSenior level~15 min

Problem

A food-delivery platform logs every order, plus a flag marking whether each customer and each courier account is suspended.

Table: `orders`

| Column | Type |
|-------------|---------|
| order_id | int |
| customer_id | int |
| courier_id | int |
| outcome | text |
| order_day | varchar |

`order_id` is the primary key. `outcome` is one of `'delivered'`, `'failed_courier'`, `'failed_customer'`. `customer_id` and `courier_id` reference `accounts.account_id`.

Table: `accounts`

| Column | Type |
|-------------|------|
| account_id | int |
| suspended | text |
| account_kind| text |

`account_id` is the primary key. `suspended` is `'yes'` or `'no'`; `account_kind` is `'customer'` or `'courier'`.

An order **counts** only if **both** its customer and its courier accounts are not suspended. The **failed rate** for a day is the number of counted orders that were not delivered divided by the total number of counted orders that day.

Write a query that reports, for each `order_day` from `'2024-02-01'` through `'2024-02-03'` that has at least one counted order, the day and its failed rate rounded to two decimals. Return columns `order_day` and `fail_rate`, in any order.

Tables

Example rows — the live problem includes the full dataset.

orders
order_idcustomer_idcourier_idoutcomeorder_day
1110delivered2024-02-01
2211failed_courier2024-02-01
3312delivered2024-02-01
accounts
account_idsuspendedaccount_kind
1nocustomer
2yescustomer
3nocustomer

Expected output

Your answer should return 3 rows with the columns order_day, fail_rate.

Starter code (SQL)

SELECT *
FROM orders;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions