# Top 5 Slowest DNS Lookups

> Five DNS lookups that took too long.

Canonical URL: <https://datadriven.io/problems/top_5_slowest_dns_lookups>

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

We are investigating DNS performance. Find the top 5 unique domain and record type pairs by their maximum latency. If pairs tie at the cutoff, include all of them. Rank from longest to shortest.

## Worked solution and explanation

### Why this problem exists in real interviews

Ranking DNS lookups by latency tests simple ordering on a performance metric. This is a warm-up that verifies you can sort by a numeric column and apply LIMIT.

---

### Break down the requirements

#### Step 1: Order by lookup duration

`ORDER BY lookup_time_ms DESC` ranks from slowest to fastest.

#### Step 2: Limit to 5

`LIMIT 5` returns the 5 slowest lookups.

---

### The solution

**Simple sort and limit for slowest lookups**

```sql
SELECT domain, lookup_time_ms, queried_at
FROM dns_lookups
ORDER BY lookup_time_ms DESC
LIMIT 5
```

> **Cost Analysis**
>
> A top-N sort is O(N log 5). An index on `lookup_time_ms DESC` allows reading the top 5 directly.

> **Interviewers Watch For**
>
> On trivial queries, interviewers assess speed and confidence.

> **Common Pitfall**
>
> Sorting ascending instead of descending. `ORDER BY lookup_time_ms ASC` gives the fastest lookups.

---

## Common follow-up questions

- How would you find the 5 slowest per domain? _(Tests PARTITION BY domain with ROW_NUMBER.)_
- What if you needed the average of the 5 slowest? _(Tests wrapping the LIMIT query and applying AVG.)_
- How would you track DNS degradation over time? _(Probes time-series analysis with GROUP BY period.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/top_5_slowest_dns_lookups)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.