Average Latency by Status

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
Uber

Problem

During a latency spike investigation, the on-call engineer needs to know whether slow responses correlate with specific HTTP status codes. Show the average latency for each status code across all API calls, walking the codes in order from the lowest to the highest.

Each status code has its own latency story.

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 11 rows
statusavg_latency
200226.2494140625
201520.78
204531.7333333333333
301542.6866666666667
400553.64

Worked solution and explanation

This is a one-key breakdown dressed up as an incident investigation. The on-call engineer does not want the average latency; they want eleven averages, one per status, side by side so a slow code stands out. Anyone can type AVG(latency). What separates candidates is fixing the grain before writing it: average the whole api_calls table and the fast 200s, which make up most of the traffic, drown out every other code, so the spike's source never shows.

Getting to the answer

Step 1: Fix the grain at one row per status

Before writing any SQL, say the output shape out loud: one row per HTTP code with its average. That decides GROUP BY status, and it rules out the single-number answer the word 'average' tempts you toward.

Step 2: Average latency within each code

AVG(latency) inside each group gives every code its own mean. Six calls have no recorded latency, and AVG skips them, so each mean covers the calls that were actually timed. Nothing about user_id or err_msg removes a row.

Step 3: Sort by the code itself

status is an integer, so ORDER BY status walks 200, 201 and 204 up through 503 numerically. Name the column avg_latency so the result reads as the lookup table the engineer asked for.

Average latency per status code
SELECT status, AVG(latency) AS avg_latency
FROM api_calls
GROUP BY status
ORDER BY status
One blended number
SELECT AVG(latency) FROM api_calls returns a single value that mixes the high-volume 200s with every other code. It cannot tell you which code is slow.
One row per code
Adding GROUP BY status returns eleven rows, each code's own mean, so any slow code stands out against the 200 baseline.

Common follow-up questions