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
Google

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

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 18 rows
msg_idchannelsender_idcontentmsg_typesent_ateditedreply_to
147#engineering197Anyone seeing increased latency?file2026-02-02 01:07:000NULL
711#engineering1361Anyone seeing increased latency?FILE2026-05-14 13:31:000NULL
1275#engineering2525Anyone seeing increased latency?system2026-08-26 01:55:000NULL
1839#engineering3689Anyone seeing increased latency?reaction2026-02-10 13:19:000NULL
2403#engineering4853Anyone seeing increased latency?text2026-05-22 01:43:000NULL

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.

Substring match on `content`
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