AnalystPath

Couriers per Depot

SQLEasyJunior level~15 min

Problem

Table: `Courier`

```text
+------------+---------+
| Column | Type |
+------------+---------+
| courier_id | int |
| depot_id | int |
+------------+---------+
courier_id is the primary key. Each row says which depot a courier works out of.
```

For each courier, report how many couriers in total belong to that courier's depot (the courier themselves included). Return `courier_id` and the count under the column `depot_headcount`. Rows may be in any order.

**Example**

```text
Courier:
+------------+----------+
| courier_id | depot_id |
+------------+----------+
| 11 | 1 |
| 12 | 1 |
| 13 | 1 |
| 21 | 2 |
+------------+----------+

Output:
+------------+-----------------+
| courier_id | depot_headcount |
+------------+-----------------+
| 11 | 3 |
| 12 | 3 |
| 13 | 3 |
| 21 | 1 |
+------------+-----------------+
```

Depot 1 has three couriers, so each of them reports 3. Depot 2 has a single courier, who reports 1.

Tables

Example rows — the live problem includes the full dataset.

Courier
courier_iddepot_id
111
121
131

Expected output

Your answer should return 4 rows with the columns courier_id, depot_headcount.

Starter code (SQL)

SELECT *
FROM Courier;

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