Find Orders That Took More Than 1.5 Times Their City's Average Shipping Time

Quick Overview

Write a SQL query that finds orders whose shipping time is more than 1.5 times the average shipping time of shipped orders in the same city, returning each order with its duration and the rounded city average. It tests date arithmetic, comparing rows against a per-group aggregate and correct handling of NULL ship dates.

Find Orders That Took More Than 1.5 Times Their City's Average Shipping Time

Company: DoorDash

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

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. ```hint Two levels of detail Each order has to be compared with a number computed from many rows of its city. Think about how to get that per-city number onto the same row as the order. ``` ### 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.

Overview: Write a SQL query that finds orders whose shipping time is more than 1.5 times the average shipping time of shipped orders in the same city, returning each order with its duration and the rounded city average. It tests date arithmetic, comparing rows against a per-group aggregate and correct handling of NULL ship dates.

DoorDash logo
DoorDash
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
order_idINTOrder identifier (primary key)
customer_idINTCustomer who placed the order, never NULL
cityVARCHAR(100)City the order belongs to, never NULL
order_dateDATEDate the order was placed, never NULL
ship_dateDATEDate the order shipped, NULL if it has not shipped

Key: order_id is the primary key.

Output Contract

Return these columns, in this order:

ColumnMeaning
order_idThe order's id
customer_idThe customer's id
cityThe order's city
order_dateDate the order was placed
ship_dateDate the order shipped
shipping_daysWhole days from order_date to ship_date, as an integer
city_avg_shipping_daysAverage 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_idcustomer_idcityorder_dateship_date
1201Denver2024-05-012024-05-01
2202Denver2024-05-022024-05-02
3203Denver2024-05-022024-05-05
4201Denver2024-05-032024-05-06
5204Austin2024-05-012024-05-03
6205Austin2024-05-022024-05-03
7206Austin2024-05-022024-05-09
8204Austin2024-05-04NULL
9207Boston2024-05-012024-05-02
10208Boston2024-05-032024-05-06

Expected result:

order_idcustomer_idcityorder_dateship_dateshipping_dayscity_avg_shipping_days
7206Austin2024-05-022024-05-0973.33
3203Denver2024-05-022024-05-0531.50
4201Denver2024-05-032024-05-0631.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.