Top Pattern Matches

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

Domain
SQL
Difficulty
medium
Seniority
L4

Problem

In our log messages, a reference number is any three characters, a dash, then at least four more characters, and it can sit anywhere in the text: at the start, in the middle or at the end of a message. Using server_logs, find the 10 servers with the most messages that contain one, along with each server's match count, most matches first and alphabetical by server name when counts tie.

A needle in a haystack, but how many haystacks?

Tables

server_logs

Columns: log_id INTEGER, server_name TEXT, message TEXT, log_timestamp TEXT, log_level TEXT, response_time_ms INTEGER

server_logs: 10 of 200 rows
log_idserver_namelog_levelmessageresponse_time_mslog_timestamp
137web-prod-02WARNConnection timeout after 30s232026-02-02 01:07:13
174api-prod-01ERRORDatabase query slow: 2.5sNULL2026-03-03 02:14:26
211api-prod-02DEBUGCache miss for key user_123492026-04-04 03:21:39
248db-primaryCRITICALMemory usage at 85%02026-05-05 04:28:52
285db-replica-01INFOSSL certificate expires in 7 daysNULL2026-06-06 05:35:05
322cache-01WARNRate limit exceeded for IP 192.168.1.x882026-07-07 06:42:18
359worker-01ERRORDisk space low: 10% remaining1012026-08-08 07:49:31
396cdn-edge-usDEBUGNew deployment v2.3.1 startedNULL2026-09-09 08:56:44
433cdn-edge-euCRITICALHealth check passed1272026-01-10 09:03:57
470monitor-01INFOFailed to connect to upstream1402026-02-11 10:10:10

Expected output

First 5 of 10 rows
server_namematch_count
cache-016
db-primary6
api-prod-024
cdn-edge-eu4
api-prod-012

Worked solution and explanation

What this is really asking

This is a positional pattern match dressed up as log forensics. Anyone can write the GROUP BY and the LIMIT. What separates candidates is pinning the shape: three characters, a literal dash, then four or more characters, anywhere in the text. Reach for %-% and every hyphenated message counts. Per-server totals balloon, and the top 10 comes back with the wrong servers and the wrong counts.


Break down the requirements

Step 1: Match the shape with LIKE

%___-____% reads as: anything, exactly 3 characters, a dash, 4 characters, anything. Each _ holds one position. The trailing % is what lets the run after the dash be longer than 4, and the leading % lets the reference sit mid-message.

Step 2: Count matching messages per server

GROUP BY server_name with COUNT(*). The WHERE filters before grouping, so the count is messages that contain a reference, not total references.

Step 3: Keep the top 10, ties by name

ORDER BY match_count DESC, server_name then LIMIT 10. The name key decides which server survives when two counts tie at the cut, so the top 10 is the same set every run.


The solution

TOP 10 SERVERS BY LIKE-PATTERN HITS
SELECT server_name, COUNT(*) AS match_count
FROM server_logs
WHERE message LIKE '%___-____%'
GROUP BY server_name
ORDER BY match_count DESC, server_name
LIMIT 10

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions