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.

The orders table records the date each order was placed (order_date) and the date it shipped (ship_date). 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. Exclude orders with a NULL ship_date from both the output and the city average. Return these columns, in this order: order_id, customer_id, city, order_date, ship_date, shipping_days (whole days from order_date to ship_date, as an integer) and city_avg_shipping_days (the 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. Table orders: order_id INTEGER (primary key), customer_id INTEGER (never NULL), city VARCHAR(100) (never NULL), order_date DATE (never NULL), ship_date DATE (NULL if the order has not shipped). 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. For example, a city average of 10/3 days gives a threshold of exactly 5 days, so a 5-day order in that city does not qualify. - A city with a single shipped order never qualifies, because that order equals its own average.

Tables

orders(order_id INTEGER, customer_id INTEGER, city VARCHAR(100), order_date DATE, ship_date DATE)

Hints

  1. 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.
  2. Leave unshipped orders out before you average, compare against the exact average, and round only the value you return.
DoorDashData ScientistmediumData Manipulation (SQL/Python)

Slow Shipments Against the City Average

The orders table records the date each order was placed (order_date) and the date it shipped (ship_date). 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. Exclude orders with a NULL ship_date from both the output and the city average.

Return these columns, in this order: order_id, customer_id, city, order_date, ship_date, shipping_days (whole days from order_date to ship_date, as an integer) and city_avg_shipping_days (the 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.

Table orders: order_id INTEGER (primary key), customer_id INTEGER (never NULL), city VARCHAR(100) (never NULL), order_date DATE (never NULL), ship_date DATE (NULL if the order has not shipped).

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. For example, a city average of 10/3 days gives a threshold of exactly 5 days, so a 5-day order in that city does not qualify.
  • A city with a single shipped order never qualifies, because that order equals its own average.

Expected Output

Tables

orders
orders
ColumnType
order_idINTEGER
customer_idINTEGER
cityVARCHAR(100)
order_dateDATE
ship_dateDATE

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions