Distribution of Tickets Closed per Shift
Problem
Table: `Shift`
```text
+------------+---------+
| Column | Type |
+------------+---------+
| agent_id | int |
| shift_day | date |
+------------+---------+
(agent_id, shift_day) is the primary key. Each row means an agent worked a
shift on that day.
```
Table: `Closure`
```text
+------------+---------+
| Column | Type |
+------------+---------+
| agent_id | int |
| closed_on | date |
| ticket_no | int |
+------------+---------+
Each row records one support ticket an agent closed on a day. An agent can
close several tickets on the same day, and may close tickets only on a day
they actually worked a shift.
```
For every shift, count how many tickets the agent closed during it. Then build a histogram: for each possible number of tickets closed, report how many shifts had exactly that count.
Return `tickets_closed` (the count value, starting from 0) and `shift_count` (how many shifts closed that many tickets). Include every integer from 0 up to the maximum number of tickets closed in any single shift, even if `shift_count` is 0. Order by `tickets_closed`.
**Example**
```text
If the per-shift ticket counts are {0, 1, 1, 2}, the output is:
tickets_closed=0 -> 1 shift, tickets_closed=1 -> 2 shifts, tickets_closed=2 -> 1 shift.
```
Tables
Example rows — the live problem includes the full dataset.
| agent_id | shift_day |
|---|---|
| 1 | 2023-01-01 |
| 1 | 2023-01-02 |
| 2 | 2023-01-01 |
| agent_id | closed_on | ticket_no |
|---|---|---|
| 1 | 2023-01-01 | 500 |
| 2 | 2023-01-01 | 501 |
| 2 | 2023-01-01 | 502 |
Expected output
Your answer should return 3 rows with the columns tickets_closed, shift_count.
Starter code (SQL)
SELECT *
FROM Shift;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