A chat product stores messages and their threaded replies in one table. A message with parent_id NULL is a root; any other message is a reply to the message whose id equals its parent_id. Threads have only one level: nobody replies to a reply.
Return every message with all of its columns plus three computed columns: its depth in the thread, the id of the thread it belongs to, and how many direct replies it has.
Input Table(s)
messages
| Column | Type | Meaning |
|---|
| id | INT | Primary key, a unique positive integer |
| parent_id | INT | NULL for a root, otherwise the id of the message being replied to (foreign key to messages.id) |
| author | VARCHAR(100) | Who wrote the message, not NULL |
| content | TEXT | Message body, not NULL |
| created_at | TIMESTAMP | When the message was posted, not NULL |
Primary key: id. parent_id references messages(id).
Output Contract
One row per message, with these columns in this order:
| Column | Value |
|---|
| id | from messages |
| parent_id | from messages |
| author | from messages |
| content | from messages |
| created_at | from messages |
| depth | 0 for a root, 1 for a direct reply |
| thread_root_id | The message's own id for a root, its parent_id for a reply |
| reply_count | Number of messages whose parent_id equals this message's id, 0 if none |
Order by thread_root_id, then depth, then created_at, then id, all ascending. No rounding is involved.
Example
messages
| id | parent_id | author | content | created_at |
|---|
| 1 | NULL | ana | Deploy is done | 2024-03-01 09:00:00 |
| 2 | 1 | ben | Thanks | 2024-03-01 09:05:00 |
| 3 | NULL | cara | Standup moved to 10 | 2024-03-01 09:10:00 |
| 4 | 1 | cara | Any errors? | 2024-03-01 09:05:00 |
| 5 | 3 | ana | Got it | 2024-03-01 09:12:00 |
| 6 | NULL | ben | Lunch? | 2024-03-01 08:30:00 |
Expected result:
| id | parent_id | author | content | created_at | depth | thread_root_id | reply_count |
|---|
| 1 | NULL | ana | Deploy is done | 2024-03-01 09:00:00 | 0 | 1 | 2 |
| 2 | 1 | ben | Thanks | 2024-03-01 09:05:00 | 1 | 1 | 0 |
| 4 | 1 | cara | Any errors? | 2024-03-01 09:05:00 | 1 | 1 | 0 |
| 3 | NULL | cara | Standup moved to 10 | 2024-03-01 09:10:00 | 0 | 3 | 1 |
| 5 | 3 | ana | Got it | 2024-03-01 09:12:00 | 1 | 3 | 0 |
| 6 | NULL | ben | Lunch? | 2024-03-01 08:30:00 | 0 | 6 | 0 |
Message 1 has two replies (2 and 4), which were posted at the same second, so id breaks the tie. Message 6 was posted first but sorts last because threads are ordered by thread_root_id, not by time. Message 6 has no replies, so its reply_count is 0.
Constraints and Clarifications
-
The table has at most 10,000 rows.
id
values are unique positive integers.
-
parent_id
is either NULL or the
id
of an existing message.
-
Only one level of nesting exists, so every reply's parent is a root and every reply has
reply_count
0.
-
author
,
content
and
created_at
are never NULL;
parent_id
is the only nullable column.
-
Ties on
created_at
within the same thread and depth are broken by
id
.
-
created_at
was a
DATETIME
column in the original MySQL schema; in PostgreSQL it is
TIMESTAMP
.