AnalystPath

Monthly Shipments and Returns by Region

SQLMediumMid level~15 min

Problem

Table: `Shipments`

```text
+------------+---------+----------+--------+-------------+
| Column | Type | ... |
+------------+---------+----------+--------+-------------+
| ship_id | int | primary key |
| region | varchar | two-letter region code |
| status | varchar | 'fulfilled' or 'cancelled' |
| value_cents| int | order value |
| ship_date | date | date the order shipped |
+------------+---------+----------+--------+-------------+
```

Table: `Returns`

```text
+-------------+---------+
| Column | Type |
+-------------+---------+
| ship_id | int |
| return_date | date |
+-------------+---------+
Each row records that the order ship_id was returned on return_date. ship_id
references Shipments.ship_id.
```

For each month-and-region combination, report the number of **fulfilled** shipments and the total value of those fulfilled shipments, alongside the number of **returns** and the total value of those returned shipments. A fulfilled shipment is counted in the month it shipped; a return is counted in the month it was returned (which may differ from the ship month).

Return `month` (formatted `YYYY-MM`), `region`, `fulfilled_count`, `fulfilled_value`, `return_count`, `return_value`. A month-region appears if it has at least one fulfilled shipment or at least one return.

Tables

Example rows — the live problem includes the full dataset.

Shipments
ship_idregionstatusvalue_centsship_date
101NWfulfilled10002022-05-18
102NWcancelled20002022-05-19
105NWfulfilled50002022-06-15
Returns
ship_idreturn_date
1022022-05-29
1012022-06-30
1052022-09-18

Expected output

Your answer should return 3 rows with the columns month, region, fulfilled_count, fulfilled_value, return_count, return_value.

Starter code (SQL)

SELECT *
FROM Shipments;

Solve this SQL question free

Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related SQL questions