Replies per Help-Desk Ticket
Problem
Table: `Messages`
```text
+-----------+------+
| Column | Type |
+-----------+------+
| msg_id | int |
| root_id | int |
+-----------+------+
There is no single primary key; the same msg_id may appear more than once.
A message with root_id = NULL is a brand-new ticket. A message with a
non-NULL root_id is a reply, and root_id is the msg_id of the ticket it
replies to.
```
For each ticket (a message whose `root_id` is NULL), report how many distinct replies it has. A reply counts once even if its `msg_id` appears multiple times. A reply whose `root_id` does not match any ticket is ignored.
Return `ticket_id` and `reply_count`, ordered by `ticket_id` ascending.
Tables
Example rows — the live problem includes the full dataset.
| msg_id | root_id |
|---|---|
| 1 | |
| 2 | |
| 1 |
Expected output
Your answer should return 3 rows with the columns ticket_id, reply_count.
Starter code (SQL)
SELECT *
FROM Messages;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