Highest Latency Endpoints
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
- City and County of San Francisco
Problem
Surface the 3 endpoints straining the API the hardest by peak latency, listing the worst offender first on down to the mildest, and show each endpoint alongside its maximum latency value.
The slowest endpoints. Everyone notices.
Tables
api_calls
Columns: call_id INTEGER, endpoint TEXT, method TEXT, status INTEGER, latency REAL, user_id INTEGER, call_time TIMESTAMP, 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 | max_latency |
|---|---|
| /api/v1/search | 1713.7 |
| /api/v2/products | 1696.4 |
| /api/v1/orders | 1679.1 |
Worked solution and explanation
This is a rank-the-groups question dressed as an incident review. Everyone writes the sort and the cut; what decides the answer is the number you sort by. Peak means each endpoint's single worst call. Reach for the average because it feels more representative and the list quietly changes: on this data the average's top 3 is /api/v1/search, /api/v1/auth/logout and /api/v1/payments, and the endpoints behind the 2nd and 3rd worst calls vanish.
Getting there
Step 1: Name the measure before you write anything
Straining the hardest by peak is MAX(latency) per endpoint, the worst call each one ever served. MAX skips the 6 calls with no recorded latency, and every status counts: the 3 worst calls on record all came back as errors, so a filter to successful calls would surface a different list.
Step 2: Collapse the calls to one row per endpoint
GROUP BY endpoint turns 670 calls into one row per logged path. /api/v1/users and /api/v1/users/ stay separate because that is how the paths were logged.
Step 3: Sort the groups, then cut
ORDER BY max_latency DESC sorts the per-endpoint peaks, worst first, and LIMIT 3 keeps the top 3. The 4th endpoint, /api/v1/users, peaks at 1661.8, below the 1679.1 in 3rd place, so the cut falls cleanly.
The solution
SELECT endpoint, MAX(latency) AS max_latency
FROM api_calls
GROUP BY endpoint
ORDER BY max_latency DESC
LIMIT 3- Ordered by average
AVG(latency)rewards endpoints with little fast traffic./api/v2/productsand/api/v1/orderseach carry 100 or more quick February calls that pull their means near 300, so they drop out of the top 3.- Ordered by peak
MAX(latency)reads only the worst call per endpoint, so volume cannot dilute it. The 1696.4 and 1679.1 spikes keep/api/v2/productsand/api/v1/ordersin 2nd and 3rd.
SELECT endpoint, AVG(latency) AS avg_latency
FROM api_calls
GROUP BY endpoint
ORDER BY avg_latency DESC
LIMIT 3Returns /api/v1/auth/logout and /api/v1/payments in place of the 2 endpoints with the worst spikes.
Common follow-up questions
- How would you report p99 latency instead of the maximum? (Tests `PERCENTILE_CONT` inside the per-endpoint summary, and why exact percentiles are costly at 120M rows.)
- How would you keep every endpoint tied for 3rd place? (Tests `DENSE_RANK()` over `max_latency` in place of `LIMIT`.)
- How would you restrict this to endpoints with at least 100 calls? (Tests `HAVING COUNT(*) >= 100` on the summarised result.)