Messages Containing Keyword
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
Problem
The on-call team is searching chat history for latency-related discussions during a recent incident. Pull all fields for messages that contain the word 'latency'.
Flagged terms in the messages.
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
| msg_id | channel | sender_id | content | msg_type | sent_at | edited | reply_to |
|---|---|---|---|---|---|---|---|
| 147 | #engineering | 197 | Anyone seeing increased latency? | file | 2026-02-02 01:07:00 | 0 | NULL |
| 194 | #incidents | 294 | PR #412 ready for review | reaction | 2026-03-03 02:14:00 | 0 | NULL |
| 241 | #random | 391 | Dashboard is down | thread_reply | 2026-04-04 03:21:00 | 0 | NULL |
| 288 | #deployments | 488 | Fixed the flaky test | system | 2026-05-05 04:28:00 | 0 | NULL |
| 335 | #data-team | 585 | Merged the migration | Text | 2026-06-06 05:35:00 | 0 | NULL |
| 382 | #general | 682 | NULL | FILE | 2026-07-07 06:42:00 | 0 | NULL |
| 429 | #engineering | 779 | LGTM | text | 2026-08-08 07:49:00 | 0 | NULL |
| 476 | #incidents | 876 | 👍 | file | 2026-09-09 08:56:00 | 0 | 5 |
| 523 | #random | 973 | reaction | 2026-01-10 09:03:00 | 0 | NULL | |
| 570 | #deployments | 1070 | Can someone check the pipeline? | thread_reply | 2026-02-11 10:10:00 | 0 | NULL |
Expected output
| msg_id | channel | sender_id | content | msg_type | sent_at | edited | reply_to |
|---|---|---|---|---|---|---|---|
| 147 | #engineering | 197 | Anyone seeing increased latency? | file | 2026-02-02 01:07:00 | 0 | NULL |
| 711 | #engineering | 1361 | Anyone seeing increased latency? | FILE | 2026-05-14 13:31:00 | 0 | NULL |
| 1275 | #engineering | 2525 | Anyone seeing increased latency? | system | 2026-08-26 01:55:00 | 0 | NULL |
| 1839 | #engineering | 3689 | Anyone seeing increased latency? | reaction | 2026-02-10 13:19:00 | 0 | NULL |
| 2403 | #engineering | 4853 | Anyone seeing increased latency? | text | 2026-05-22 01:43:00 | 0 | NULL |
Worked solution and explanation
This is a substring search dressed up as incident forensics. Nobody posts 'latency' as a whole message: on this table it only ever appears inside a sentence, 'Anyone seeing increased latency?', so the skill is finding a word anywhere inside free text. The trap is reading 'contain the word' as equality. content = 'latency', or an anchored LIKE 'latency%', runs cleanly and returns zero rows. The on-call team then concludes nobody talked about latency during the incident, and the postmortem loses all 18 real messages.
Getting to the query
Step 1: Search content, and ignore msg_type
The column that jumps out is msg_type, which holds 'file', 'FILE', 'Text' and 'text'. That messiness invites cleanup, but it has nothing to do with the question: the word lives in content. Normalizing msg_type here is effort spent on the wrong field.
Step 2: Put a wildcard on both sides
'%latency%' matches the word at the start, the middle or the end of a message. 'latency%' only matches messages that open with it. '%latency' only matches messages that end with it, and the real message ends in a question mark, so even that misses every row.
Step 3: Return all eight columns by name
The ask is every field, untouched: no counting and no reshaping. List the eight columns explicitly, so the output contract is visible in the query and survives a schema change.
SELECT msg_id, channel, sender_id, content, msg_type, sent_at, edited, reply_to
FROM chat_msgs
WHERE content LIKE '%latency%'- Exact match
WHERE content = 'latency'compares the whole value. No message on the table is the bare word, so the result is empty and looks like a clean answer.- Substring match
WHERE content LIKE '%latency%'finds the word anywhere in the sentence: 18 messages, every one of them 'Anyone seeing increased latency?'.
Common follow-up questions
- The incident ran for two hours on one day. How do you restrict the search to that window, and what does it do to the scan? (Tests partition pruning on `sent_at` ahead of the expensive substring filter.)
- How would you stop 'latency' from matching inside a longer token like 'latencyBudget'? (Tests word-boundary matching with a regular expression instead of a bare wildcard.)
- How would you also pull every reply to the latency messages? (Tests relating `chat_msgs` to itself through `reply_to`.)