Business Analytics · Problem 87 of 118

Shipping Speed by City

HardBusiness AnalyticsShop data

For shipped orders (shipped_on is not NULL), the days to ship are the days between order_date and shipped_on.

Return city (ship_city in lower case), shipped_orders and avg_days (average days to ship, rounded to 1 decimal) for cities with at least 100 shipped orders. Sort by avg_days from low to high, then city.

Hint 1

Group on LOWER(ship_city) so that Lima, lima and LIMA form one city.

Hint 2

JULIANDAY(shipped_on) - JULIANDAY(order_date) is the days to ship of one order; average that.

Reference solution

SELECT LOWER(ship_city) AS city,
       COUNT(*) AS shipped_orders,
       ROUND(AVG(JULIANDAY(shipped_on) - JULIANDAY(order_date)), 1) AS avg_days
FROM orders
WHERE shipped_on IS NOT NULL
GROUP BY LOWER(ship_city)
HAVING COUNT(*) >= 100
ORDER BY avg_days, city;

Real data is messy, and the cleaning decides the answer: grouping by the raw ship_city splits one city into several small groups, which changes counts, averages and even which cities pass the HAVING threshold. Clean first (LOWER, and TRIM if needed), group on the cleaned expression, and keep WHERE for row-level filters (unshipped orders) and HAVING for group-level ones (at least 100 orders).