Messages From Specific Users

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 received a complaint about users 2 and 3. Pull all chat messages where either user sent the message, or where the content mentions their user IDs.

Specific users. What did they say?

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 5 of 50 rows
msg_idchannelsender_idcontentmsg_typesent_ateditedreply_to
194#incidents294PR #412 ready for reviewreaction2026-03-03 02:14:000NULL
617#data-team1167Rolling back to v2.0system2026-03-12 11:17:000NULL
664#general1264Deploying v2.1 to prodText2026-04-13 12:24:00111
758#incidents1458PR #412 ready for reviewtext2026-06-15 14:38:000NULL
1181#data-team2331Rolling back to v2.0reaction2026-06-24 23:41:000NULL

Worked solution and explanation

Why this problem exists in real interviews

The chat_msgs table contains channel and content values that must be processed with pattern matching. This appears as a fundamentals check to probe whether you reason about the correct aggregation grain before writing any window or GROUP BY clause. Getting the column references right on the first try is what interviewers watch for.


Break down the requirements

Step 1: Read from chat_msgs

The query targets chat_msgs with 8 columns. Identify which columns are needed for the output.

Step 2: Filter to the target rows

Apply the LIKE pattern match in the WHERE clause. This narrows the dataset before any grouping or aggregation.

Step 3: Return the result set

Select the required columns with any necessary aliasing or formatting.


The solution

OR-based multi-condition filter
SELECT msg_id, channel, sender_id, content, msg_type, sent_at, edited, reply_to
FROM chat_msgs
WHERE sender_id IN (2, 3)
    OR content LIKE '%2%'
    OR content LIKE '%3%'

Common follow-up questions