Monthly Shipments and Returns by Region
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.
| ship_id | region | status | value_cents | ship_date |
|---|---|---|---|---|
| 101 | NW | fulfilled | 1000 | 2022-05-18 |
| 102 | NW | cancelled | 2000 | 2022-05-19 |
| 105 | NW | fulfilled | 5000 | 2022-06-15 |
| ship_id | return_date |
|---|---|
| 102 | 2022-05-29 |
| 101 | 2022-06-30 |
| 105 | 2022-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