cswork / libraryOpen practice workspace

Coding questions · Amazon

Calculate Defect Rate and Identify Top Lanes for Carriers

This question evaluates competency in SQL-based data manipulation for a data scientist role, covering joins, aggregations, date-based filtering, metric computation, and use of window functions to derive week-over-week changes.

shipment

+-------------+----------+-----------+---------+---------+-------------+-----------+
| shipment_id | order_id | ship_date | carrier | origin  | destination | status    |
+-------------+----------+-----------+---------+---------+-------------+-----------+
| 1001        | 5001     | 2023-09-01| UPS     | SEA     | NYC         | Delivered |
| 1002        | 5002     | 2023-09-02| FedEx   | LAX     | CHI         | In-Transit|
| 1003        | 5003     | 2023-09-03| USPS    | DAL     | ATL         | Delivered |
+-------------+----------+-----------+---------+---------+-------------+-----------+

defect

+-----------+-------------+-------------+----------------------+
| defect_id | shipment_id | defect_type | defect_reported_date |
+-----------+-------------+-------------+----------------------+
| 1         | 1001        | Damaged     | 2023-09-05           |
| 2         | 1002        | Lost        | 2023-09-06           |
| 3         | 1002        | LateDelivery| 2023-09-07           |
+-----------+-------------+-------------+----------------------+
Scenario

Amazon Logistics team wants to monitor and reduce shipment defects across carriers and shipment lanes.

Question

Given tables shipment and defect, write SQL to calculate the defect rate (number of defects ÷ number of shipments) for each carrier in the last 30 days. Extend your query to show the week-over-week percentage change in defect rate for each carrier using window functions. Identify the top 3 origin-destination lanes with the highest count of 'Damaged' defects in the past quarter.

Hints

Join shipment and defect, filter by date, aggregate counts, compute rates, use LAG for WoW change.

Shipment Defect Rates and Top Damaged Lanes

Using the shipment and defect tables, assume the analysis date is 2025-06-01. Return one combined result set with columns result_set, carrier, shipments, defects, defect_rate, week_start, wow_change_pct, origin, destination, and damaged_defect_count:

  1. result_set = 'carrier_defect_rate_30d': for shipments from 2025-05-02 through 2025-06-01 inclusive, calculate defects divided by shipments for each carrier.
  2. result_set = 'carrier_defect_rate_wow_30d': for the same period, compute weekly defect rate by carrier using Monday-start DATE_TRUNC('week', ship_date), plus week-over-week percentage change in defect rate using LAG.
  3. result_set = 'top_damaged_lanes_past_quarter': for 2025-03-01 through 2025-06-01 inclusive, return the top 3 origin-destination lanes by count of 'Damaged' defects.

Fields that do not apply to a row should be NULL.

Table shipment

columntypeconstraints
shipment_idINTEGERPRIMARY KEY, NOT NULL
order_idINTEGERNOT NULL
ship_dateDATENOT NULL
carrierVARCHAR(20)NOT NULL
originVARCHAR(3)NOT NULL
destinationVARCHAR(3)NOT NULL
statusVARCHAR(20)NOT NULL

Sample data:

carrierdestinationorder_idoriginship_dateshipment_idstatus
UPSNYC5001SEA2025-05-051001Delivered
FedExCHI5002LAX2025-05-061002In-Transit
USPSATL5003DAL2025-05-071003Delivered

Table defect

columntypeconstraints
defect_idINTEGERPRIMARY KEY, NOT NULL
shipment_idINTEGERNOT NULL, FOREIGN KEY REFERENCES shipment(shipment_id)
defect_typeVARCHAR(50)NOT NULL
defect_reported_dateDATENOT NULL

Sample data:

defect_iddefect_reported_datedefect_typeshipment_id
12025-05-10Damaged1001
22025-05-11Lost1002
32025-05-12LateDelivery1002

Expected output

One combined result set for carrier defect rate, weekly defect rate with week-over-week change, and top damaged lanes.

carrierdamaged_defect_countdefect_ratedefectsdestinationoriginresult_setshipmentsweek_startwow_change_pct
FedExNULL22NULLNULLcarrier_defect_rate_30d1NULLNULL
UPSNULL11NULLNULLcarrier_defect_rate_30d1NULLNULL
USPSNULL00NULLNULLcarrier_defect_rate_30d1NULLNULL
FedExNULL22NULLNULLcarrier_defect_rate_wow_30d12025-05-05NULL
UPSNULL11NULLNULLcarrier_defect_rate_wow_30d12025-05-05NULL
USPSNULL00NULLNULLcarrier_defect_rate_wow_30d12025-05-05NULL
NULL1NULLNULLNYCSEAtop_damaged_lanes_past_quarterNULLNULLNULL

Hints

  1. Use COUNT(DISTINCT shipment_id) for shipment counts when joining to defects.
  2. DATE_TRUNC('week', ship_date) gives Monday-start weeks in PostgreSQL.
  3. Use LAG over carrier and week_start for week-over-week change.