Compute thread root, depth and reply count for threaded messages

Quick Overview

Write a SQL query over a self-referencing messages table that returns each message with its thread depth, its thread root id and its number of direct replies, in thread order. It tests the adjacency list pattern, self-joins with aggregation, NULL handling and deterministic ordering.

Compute thread root, depth and reply count for threaded messages

Company: Slack

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

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. ```hint Where the count lives A message's reply count is not stored on its own row. It comes from the other rows of the same table whose `parent_id` points at it. ``` ### 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`.

Overview: Write a SQL query over a self-referencing messages table that returns each message with its thread depth, its thread root id and its number of direct replies, in thread order. It tests the adjacency list pattern, self-joins with aggregation, NULL handling and deterministic ordering.

Slack logo
Slack
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

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

ColumnTypeMeaning
idINTPrimary key, a unique positive integer
parent_idINTNULL for a root, otherwise the id of the message being replied to (foreign key to messages.id)
authorVARCHAR(100)Who wrote the message, not NULL
contentTEXTMessage body, not NULL
created_atTIMESTAMPWhen 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:

ColumnValue
idfrom messages
parent_idfrom messages
authorfrom messages
contentfrom messages
created_atfrom messages
depth0 for a root, 1 for a direct reply
thread_root_idThe message's own id for a root, its parent_id for a reply
reply_countNumber 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

idparent_idauthorcontentcreated_at
1NULLanaDeploy is done2024-03-01 09:00:00
21benThanks2024-03-01 09:05:00
3NULLcaraStandup moved to 102024-03-01 09:10:00
41caraAny errors?2024-03-01 09:05:00
53anaGot it2024-03-01 09:12:00
6NULLbenLunch?2024-03-01 08:30:00

Expected result:

idparent_idauthorcontentcreated_atdepththread_root_idreply_count
1NULLanaDeploy is done2024-03-01 09:00:00012
21benThanks2024-03-01 09:05:00110
41caraAny errors?2024-03-01 09:05:00110
3NULLcaraStandup moved to 102024-03-01 09:10:00031
53anaGot it2024-03-01 09:12:00130
6NULLbenLunch?2024-03-01 08:30:00060

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 .