Average Search Endpoint Latency

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
Forbes

Problem

Autocomplete feels sluggish and the frontend team suspects the search backend is the bottleneck. What is the average latency for API calls hitting the '/api/v1/search' endpoint?

One endpoint. Average speed.

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 1 of 1 rows
avg_latency
347.89294117647063

Worked solution and explanation

This is a filtered aggregate wearing an incident costume. The whole skill is scoping: AVG(latency) must see only the '/api/v1/search' rows, so the endpoint predicate narrows api_calls before the average is taken. Everyone gets the shape right. What separates candidates is not letting the question drift into a table-wide number, and knowing what the average does with a call that was never timed. Drop the filter and you hand the frontend team a latency blended across every route, a number that can neither clear nor convict the search backend.

Building it

Step 1: Pin the rows to the exact search route

Use equality: endpoint = '/api/v1/search'. The table carries near-miss routes like '/api/v1/users/' and '/api/v2/products/', plus padded spellings of other endpoints, so a loose pattern is the habit that eventually sweeps a sibling route into a latency number. Equality is exact and the cheapest predicate there is.

Step 2: Average only what was timed

AVG(latency) skips NULLs. Of the 86 search calls, one has no latency recorded, so the mean is taken over 85 timed calls. That is the right reading: an untimed call is not a zero-latency call, and wrapping it in COALESCE(latency, 0) would quietly make search look faster than it is.

Step 3: Return one number

The question asks for a single figure, so the filtered rows collapse into one row named avg_latency. No grouping and no sorting: a GROUP BY endpoint would add a column nobody asked for, and there is only one row to order.

Average latency of the search endpoint
SELECT AVG(latency) AS avg_latency
FROM api_calls
WHERE endpoint = '/api/v1/search'
Whole table
SELECT AVG(latency) FROM api_calls averages every call to every route: orders, users, products, auth and search together. It runs, it returns one tidy number, and that number describes the whole API rather than autocomplete.
Search only
WHERE endpoint = '/api/v1/search' narrows the input first, so the 85 timed search calls are the only ones AVG ever sees, and the answer is about 347.89.

Common follow-up questions