Top 5 Slowest DNS Lookups

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
L3

Problem

List the domain and record type pairs with the five longest worst-case lookups in dns_lookups, slowest first, for the network team's latency review. If pairs tie for fifth, keep every one of them.

Five DNS lookups that took too long.

Tables

dns_lookups

Columns: lookup_id INTEGER, domain TEXT, rec_type TEXT, ttl_secs INTEGER, status TEXT, latency REAL, looked_at TEXT

dns_lookups: 10 of 200 rows
lookup_iddomainrec_typettl_secsstatuslatencylooked_at
129cdn.example.comAAAA47NXDOMAIN0.472026-02-02 01:03:00
158auth.example.comCNAME64SERVFAIL0.842026-03-03 02:06:00
187search.example.comMX81TIMEOUT1.212026-04-04 03:09:00
216db.internal.netTXT98REFUSED1.582026-05-05 04:12:00
245cache.internal.netNS115NOERROR1.952026-06-06 05:15:00
274example.comA132NXDOMAIN2.322026-07-07 06:18:00
303www.example.comAAAA149SERVFAIL2.692026-08-08 07:21:00
332API.example.comCNAMENULLTIMEOUT3.062026-09-09 08:24:00
361Cdn.Example.ComMX183REFUSED3.432026-01-10 09:27:00
390api.example.comTXT200NOERRORNULL2026-02-11 10:30:00

Expected output

First 5 of 5 rows
domainrec_typemax_latency
Cdn.Example.ComMX36.73
API.example.comCNAME36.36
www.example.comAAAA35.99
example.comA35.62
cache.internal.netNS35.25

Worked solution and explanation

This is a top five of groups dressed up as a top five of lookups. Anyone can sort by latency. The skill being probed is whether you collapse each domain and rec_type pair to its single worst lookup before you cut the list. Skip that and dns_lookups hands you the same pair twice. Every lookup in this log is recorded twice at the same latency, so the raw top five covers three pairs, and two of the real offenders never make the report.

What the raw top five returns

The five slowest lookups, not the five slowest pairs
SELECT domain, rec_type, latency
FROM dns_lookups
ORDER BY latency DESC
LIMIT 5;

Returns Cdn.Example.Com MX twice, API.example.com CNAME twice and www.example.com AAAA once: three pairs in five rows.

Building the answer

Step 1: Collapse each pair to its worst lookup

Group dns_lookups by domain and rec_type and take MAX(latency) as max_latency. The question is about a pair's worst case, so one row per pair is the grain everything after this works on. MAX skips NULLs, so a pair keeps its slowest timed lookup even when some of its lookups never recorded one.

Step 2: Number the pairs by worst case, ties sharing a place

DENSE_RANK() OVER (ORDER BY MAX(latency) DESC) runs after the grouping, so it sees thirty pairs, not two hundred lookups. Pairs with the same maximum get the same number, and the next value takes the next number. That is exactly what keeping every pair tied for fifth asks for.

Step 3: Cut at five, then sort slowest first

A window result cannot be filtered in the WHERE of the query that computes it, so the pair_worst CTE holds the numbered pairs and the outer query keeps latency_rank <= 5. Then ORDER BY max_latency DESC delivers slowest first. That is the only order promised, so no tiebreaker is owed.

Worst case per pair, tie-aware top five
WITH pair_worst AS (
  SELECT domain,
         rec_type,
         MAX(latency) AS max_latency,
         DENSE_RANK() OVER (ORDER BY MAX(latency) DESC) AS latency_rank
  FROM dns_lookups
  GROUP BY domain, rec_type
)
SELECT domain, rec_type, max_latency
FROM pair_worst
WHERE latency_rank <= 5
ORDER BY max_latency DESC;
A fixed five rows
ORDER BY max_latency DESC LIMIT 5 after the grouping returns exactly five rows. It matches today only because no two pairs share a maximum. The day two pairs tie for fifth, one is dropped, and the engine decides which.
Five worst-case values
DENSE_RANK() with latency_rank <= 5 keeps every pair holding one of the five longest worst cases. A tie for fifth lengthens the list instead of silently truncating it.

Common follow-up questions