# Lowest Latency per Service

> The fastest response each service ever gave.

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

Domain: SQL · Difficulty: medium · Seniority: L3

## Problem

For each service in the us-east-1 region, find the lowest latency recorded. Results should appear by latency descending, then alphabetically by service name.

## Worked solution and explanation

### Why this problem exists in real interviews

The svc_health table contains svc_name and status values that must be processed with grouping. This appears in mid-level screens to probe whether you reason about the correct aggregation grain before writing any window or GROUP BY clause.

> **Trick to Solving**
>
> Read the prompt carefully for implicit constraints. The phrase structure hints at the grain of the output: what each row represents.
> 
> 1. Identify the output grain from the prompt (one row per what?)
> 2. Work backward from the desired output columns
> 3. Build the query inside-out: innermost subquery first, then layer on filters and aggregates

---

### Break down the requirements

#### Step 1: Filter to the target rows

Apply the `WHERE` filter to restrict the working set before aggregation. Filtering early reduces the number of rows that downstream operations process.

#### Step 2: Aggregate with MIN

Group by the output grain and apply `MIN()` to compute the metric. The `GROUP BY` must match exactly what the output needs: one row per group key.

#### Step 3: Order the final output

Apply `ORDER BY` as specified to produce the expected row sequence. When tied values exist, add a secondary sort column for determinism.

---

### The solution

**Filtered MIN with compound ORDER BY**

```sql
SELECT svc_name, MIN(latency) AS min_latency
FROM svc_health
WHERE region = 'us-east-1'
GROUP BY svc_name
ORDER BY min_latency DESC, svc_name ASC
```

> **Cost Analysis**
>
> The query scans 35M rows from `svc_health`.

> **Interviewers Watch For**
>
> Candidates who verbalize their approach before typing, naming the output columns and expected row count, consistently perform better.

> **Common Pitfall**
>
> Returning more columns than the prompt asks for can trigger a "wrong schema" failure in automated grading. Match the output specification exactly.

---

## Common follow-up questions

- What happens to your result if svc_health.latency contains NULLs for some rows? _(Tests whether the candidate accounts for NULL behavior in aggregates and comparisons on latency.)_
- How would you verify that your aggregation on svc_health.check_id is not double-counting due to duplicate rows? _(Tests data quality awareness and deduplication strategies.)_
- With millions of distinct values in svc_health.check_id, what index strategy would you use to keep this query performant? _(Tests indexing knowledge specific to high-cardinality columns like check_id.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/lowest_latency_per_service)
- [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.