You are given an orders table with the date each order was placed and the date it shipped.
Find orders whose shipping duration (days between ship_date and order_date) is more than 1.5 times the average shipping duration of all orders in the same city. Return order_id, customer_id, city, order_date, ship_date, shipping_days, and city_avg_shipping_days (rounded to 2 decimals), ordered by city then order_id ascending.
Exclude orders with a NULL ship_date from both the output and the city average.
Input Table(s)
orders
| Column | Type | Meaning |
|---|
| order_id | INT | Order identifier (primary key) |
| customer_id | INT | Customer who placed the order, never NULL |
| city | VARCHAR(100) | City the order belongs to, never NULL |
| order_date | DATE | Date the order was placed, never NULL |
| ship_date | DATE | Date the order shipped, NULL if it has not shipped |
Key: order_id is the primary key.
Output Contract
Return these columns, in this order:
| Column | Meaning |
|---|
| order_id | The order's id |
| customer_id | The customer's id |
| city | The order's city |
| order_date | Date the order was placed |
| ship_date | Date the order shipped |
| shipping_days | Whole days from order_date to ship_date, as an integer |
| city_avg_shipping_days | Average shipping_days over the shipped orders in the same city, rounded to 2 decimal places |
Sort by city ascending, then by order_id ascending. Because order_id is unique, this order is fully determined.
Example
orders
| order_id | customer_id | city | order_date | ship_date |
|---|
| 1 | 201 | Denver | 2024-05-01 | 2024-05-01 |
| 2 | 202 | Denver | 2024-05-02 | 2024-05-02 |
| 3 | 203 | Denver | 2024-05-02 | 2024-05-05 |
| 4 | 201 | Denver | 2024-05-03 | 2024-05-06 |
| 5 | 204 | Austin | 2024-05-01 | 2024-05-03 |
| 6 | 205 | Austin | 2024-05-02 | 2024-05-03 |
| 7 | 206 | Austin | 2024-05-02 | 2024-05-09 |
| 8 | 204 | Austin | 2024-05-04 | NULL |
| 9 | 207 | Boston | 2024-05-01 | 2024-05-02 |
| 10 | 208 | Boston | 2024-05-03 | 2024-05-06 |
Expected result:
| order_id | customer_id | city | order_date | ship_date | shipping_days | city_avg_shipping_days |
|---|
| 7 | 206 | Austin | 2024-05-02 | 2024-05-09 | 7 | 3.33 |
| 3 | 203 | Denver | 2024-05-02 | 2024-05-05 | 3 | 1.50 |
| 4 | 201 | Denver | 2024-05-03 | 2024-05-06 | 3 | 1.50 |
Austin's shipped orders took 2, 1 and 7 days. Order 8 has not shipped, so it is left out, and the average is 10 / 3 = 3.33... days. Only order 7 is above 1.5 times that, which is 5 days. Denver's orders took 0, 0, 3 and 3 days, an average of 1.5 with a threshold of 2.25, so both 3-day orders qualify. Boston's orders took 1 and 3 days, an average of 2 and a threshold of exactly 3. The 3-day order is not more than 3, so Boston returns nothing. Austin comes first because the result is sorted by city before order_id.
Constraints and Clarifications
-
orders
has at most 10,000 rows.
-
ship_date >= order_date
whenever
ship_date
is not NULL, so
shipping_days
is never negative. An order shipped on the day it was placed has
shipping_days = 0
.
-
Each
city
has at least one shipped order.
-
An order with a NULL
ship_date
neither appears in the output nor counts toward its city's average.
-
The comparison is strict: an order at exactly 1.5 times its city average is excluded.
-
Compare against the exact city average. Rounding applies only to the returned
city_avg_shipping_days
.
-
A city with a single shipped order never qualifies, because that order equals its own average.