The Long Wait
An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Spotify
Problem
An endpoint's speed is judged by its average latency across every call it served, never by its single slowest call. List each endpoint in api_calls with that average, slowest first.
Every route runs at its own pace. Find the ones lagging.
Tables
api_calls
Columns: call_id INTEGER, endpoint TEXT, method TEXT, status INTEGER, latency REAL, user_id INTEGER, call_time TEXT, err_msg TEXT
| call_id | endpoint | method | status | latency | user_id | call_time | err_msg |
|---|---|---|---|---|---|---|---|
| 8079 | /api/v1/orders | POST | 200 | 4.2 | 100 | 2026-02-02 01:07:00 | NULL |
| 8158 | /api/v2/products | PUT | 200 | 7.9 | 197 | 2026-03-03 02:14:00 | NULL |
| 8237 | /api/v1/search | DELETE | 201 | 11.6 | 294 | 2026-04-04 03:21:00 | NULL |
| 8316 | /api/v1/auth/login | PATCH | 204 | 15.3 | 391 | 2026-05-05 04:28:00 | NULL |
| 8395 | /api/v1/auth/logout | get | 301 | 19 | NULL | 2026-06-06 05:35:00 | NULL |
| 8474 | /api/v2/analytics | post | 400 | 22.7 | 585 | 2026-07-07 06:42:00 | Not found |
| 8553 | /api/v1/payments | GET | 401 | 26.4 | 682 | 2026-08-08 07:49:00 | Unauthorized |
| 8632 | /api/v1/users/ | POST | 403 | 30.1 | 779 | 2026-09-09 08:56:00 | Rate limit exceeded |
| 8711 | /api/v2/products/ | PUT | 404 | 33.8 | 876 | 2026-01-10 09:03:00 | Internal server error |
| 8790 | /api/v1/users | DELETE | 500 | 37.5 | NULL | 2026-02-11 10:10:00 | Bad gateway |
Expected output
| endpoint | avg_latency |
|---|---|
| /api/v1/search | 347.89294117647063 |
| /api/v1/auth/logout | 342.712962962963 |
| /api/v1/payments | 316.21666666666664 |
| /api/v1/auth/login | 309.6337837837838 |
| /api/v1/orders | 303.88257575757575 |
Worked solution and explanation
What this really asks
This is a per-group average wearing an SLO costume. The skill being probed: can you collapse 670 call rows into one latency figure per endpoint, then sort the endpoints by that figure and not by anything stored on a row. Anyone can type AVG(latency). What separates candidates is what they sort on. Order by the raw latency column and you are ordering individual calls, so one slow call on a quick route lands above a route that is slow every time.
The other trap: average everything and you hand back one platform number when the question was which routes are slow. Put endpoint in the select list but leave it out of the grouping, and the engine either errors or, worse in a lax dialect, staples an arbitrary endpoint to a global average that looks plausible and is wrong.
The one decision that matters
Step 1: Group at the endpoint grain
Collapse the rows with GROUP BY endpoint and let AVG(latency) run inside each group. That one choice is the difference between a per-route breakdown and a meaningless global scalar. Every non-aggregated column you select must appear in the grouping.
Step 2: Order so the slow routes surface
The worst offenders belong on top, so sort by avg_latency descending. Sorting by the aggregate is the natural reading of 'slowest first', and it is what makes the result actionable instead of merely correct.
SELECT endpoint, AVG(latency) AS avg_latency
FROM api_calls
GROUP BY endpoint
ORDER BY avg_latency DESC- Global average (the mistake)
SELECT AVG(latency) FROM api_callsreturns one number for the whole platform. It answers a question nobody asked and hides the slow routes inside the mean.- Per-endpoint average (the answer)
GROUP BY endpointkeeps one row per route, so '/api/v1/search', averaging about 347.9, surfaces on top instead of disappearing into the platform-wide mean.
Common follow-up questions
- How would you restrict this to only the last 7 days of calls, and where does that filter go? (Tests time-window reasoning and where a filter belongs relative to the aggregate.)
- The SLO is defined on p95 latency, not the mean. How would you compute a per-endpoint p95 instead? (Tests understanding that the mean hides tail behavior, which is what SLOs actually track.)
- If '/api/v1/users' and '/api/v1/users/' should be one endpoint, how do you fold them together before grouping? (Tests path normalization and grouping on a derived expression.)