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

api_calls: 10 of 670 rows
call_idendpointmethodstatuslatencyuser_idcall_timeerr_msg
8079/api/v1/ordersPOST2004.21002026-02-02 01:07:00NULL
8158/api/v2/productsPUT2007.91972026-03-03 02:14:00NULL
8237/api/v1/searchDELETE20111.62942026-04-04 03:21:00NULL
8316/api/v1/auth/loginPATCH20415.33912026-05-05 04:28:00NULL
8395/api/v1/auth/logoutget30119NULL2026-06-06 05:35:00NULL
8474/api/v2/analyticspost40022.75852026-07-07 06:42:00Not found
8553/api/v1/paymentsGET40126.46822026-08-08 07:49:00Unauthorized
8632/api/v1/users/POST40330.17792026-09-09 08:56:00Rate limit exceeded
8711/api/v2/products/PUT40433.88762026-01-10 09:03:00Internal server error
8790/api/v1/usersDELETE50037.5NULL2026-02-11 10:10:00Bad gateway

Expected output

First 5 of 12 rows
endpointavg_latency
/api/v1/search347.89294117647063
/api/v1/auth/logout342.712962962963
/api/v1/payments316.21666666666664
/api/v1/auth/login309.6337837837838
/api/v1/orders303.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.

Per-endpoint average, slowest first
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_calls returns 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 endpoint keeps 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