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 3 characters, a dash, then at least 4 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, log_level TEXT, message TEXT, response_time_ms INTEGER, log_timestamp TIMESTAMP

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 containment match dressed up as log forensics. Anyone can write the GROUP BY and the LIMIT. What separates candidates is writing the shape as something the message contains: 3 characters, a literal dash, 4 or more characters, with % open on both sides. Every reference in this log sits mid-message ('Trace ID: abc-defgh matched'), so drop either outer % and the query returns nothing. The second catch is the cut: 4 servers tie at 2 matches for the last 3 places, and only the name tie-break decides which one falls off.


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 of messages that contain a reference; one holding two references still counts once.

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