Threads Excluding User

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3
Asked in
Meta

Problem

The trust and safety team is auditing reply activity in chat_msgs, where one conversation is a single sender_id paired with the channel they posted in. Looking only at reply messages (those with a non-null reply_to), count how many different sender-and-channel conversations there are.

Every thread they're not part of.

Tables

chat_msgs

Columns: msg_id INTEGER, channel TEXT, sender_id INTEGER, content TEXT, msg_type TEXT, sent_at TEXT, edited INTEGER, reply_to INTEGER

chat_msgs: 10 of 200 rows
msg_idchannelsender_idcontentmsg_typesent_ateditedreply_to
147#engineering197Anyone seeing increased latency?file2026-02-02 01:07:000NULL
194#incidents294PR #412 ready for reviewreaction2026-03-03 02:14:000NULL
241#random391Dashboard is downthread_reply2026-04-04 03:21:000NULL
288#deployments488Fixed the flaky testsystem2026-05-05 04:28:000NULL
335#data-team585Merged the migrationText2026-06-06 05:35:000NULL
382#general682NULLFILE2026-07-07 06:42:000NULL
429#engineering779LGTMtext2026-08-08 07:49:000NULL
476#incidents876👍file2026-09-09 08:56:0005
523#random973reaction2026-01-10 09:03:000NULL
570#deployments1070Can someone check the pipeline?thread_reply2026-02-11 10:10:000NULL

Expected output

First 1 of 1 rows
conversation_count
24

Worked solution and explanation

What this really is

This is a distinct composite-key count wearing a trust-and-safety costume. Strip the business words and the ask is: build one key per (sender_id, channel) pair, then count the distinct keys, but only over the rows that are actually replies. Anyone can COUNT the table. Two things separate the answers. You have to dedup the pair, because the same sender keeps posting in the same channel and raw rows overcount wildly. And you have to drop the roughly 70 percent of rows that are not replies, because they carry sender-channel pairs that never answered anyone. Miss either and the audit reports a number that is confidently wrong.


The two things that bite

Step 1: The non-reply majority has to go

reply_to is null on about 70 percent of the 30M rows: those are original messages, not replies. If you leave them in, every sender-and-channel pair they touch enters the count, even pairs that never once replied. The filter is not cosmetic. It is the difference between counting reply conversations and counting all activity. Write WHERE reply_to IS NOT NULL and the non-reply majority is gone.

Step 2: Rows are not conversations

A busy sender posts twenty-odd times, often in the same channel, so the raw row count of replies has nothing to do with the number of conversations. Wrapping the composite key in COUNT(DISTINCT ...) collapses those repeats: one sender-and-channel pairing counts once no matter how many reply messages it spans. Drop the DISTINCT and you are back to counting messages.

Step 3: Mind the seam between int and string

sender_id is an integer and channel is a string. Glue them with nothing between and (sender 197, channel '#a') can, in the general case, collide with a differently split pair. A single non-numeric delimiter between the two values keeps the boundary unambiguous. Here the channels all start with '#', so a plain '-' is safe, but making the separator explicit is the habit that survives a schema where it is not.


The solution

Distinct composite key over the reply rows only
SELECT COUNT(DISTINCT sender_id || '-' || channel) AS conversation_count
FROM chat_msgs
WHERE reply_to IS NOT NULL

Common follow-up questions