Low Latency API Calls
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
- British Airways
Problem
A call that took exactly 50 milliseconds still counts as fast in our baseline, and so does anything quicker. Pull every qualifying call record from api_calls in full, fastest first.
Fast endpoints. Confirmed fast.
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
| call_id | endpoint | method | status | latency | user_id | call_time | err_msg |
|---|---|---|---|---|---|---|---|
| 16000 | /api/v1/users | GET | 200 | 1 | 4950 | 2023-01-01 00:00:00 | NULL |
| 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 |
| 21000 | /api/v1/users | GET | 200 | 10 | 2137 | 2026-02-01 00:00:00 | NULL |
| 8474 | /api/v2/analytics | post | 400 | 22.7 | 585 | 2026-07-07 06:42:00 | Not found |
Worked solution and explanation
What this problem is really checking
This is a boundary-reading test dressed up as a performance baseline. Anyone can write a WHERE and an ORDER BY against api_calls; the whole problem lives in one character. 'Exactly 50 milliseconds still counts' means latency <= 50, and the reflexive latency < 50 runs cleanly, returns plausible rows, and quietly drops the call sitting on the line. On this data that call is real: the successful request that ended a burst of 500s on '/api/v2/analytics' came back in exactly 50.0 ms, so the strict version comes up one row short.
Break down the requirements
Step 1: Filter with the inclusive operator
Read the threshold phrase word by word. A call at exactly 50 is fast, and so is anything quicker, so the predicate is latency <= 50. Before moving on, plug 50 itself into the condition and confirm it passes.
Step 2: Return the whole record
The team wants each call record in full, so the projection lists all 8 columns of api_calls in table order. Spelling them out keeps the query readable and stable if someone later adds a column to the table.
Step 3: Order fastest first
Sort on latency ascending. Nothing is cut from the result, so equally fast calls may come back in any order and the ORDER BY needs no second key.
The solution
SELECT
call_id,
endpoint,
method,
status,
latency,
user_id,
call_time,
err_msg
FROM api_calls
WHERE latency <= 50
ORDER BY latency ASCOne scan, one inclusive predicate, one sort on latency.
- Strict cutoff
WHERE latency < 50drops the 50.0 ms call. No error, no warning, and the rows it does return look right, which is exactly why the mistake survives review.- Inclusive cutoff
WHERE latency <= 50keeps the boundary call, matching the definition the team wrote: exactly 50 still counts.
SELECT
call_id,
endpoint,
method,
status,
latency,
user_id,
call_time,
err_msg
FROM api_calls
WHERE latency < 50
ORDER BY latency ASCOne character different, and the 50 ms call is gone.
Common follow-up questions
- What should happen to calls whose `latency` is `NULL`, and does your query already handle them? (Checks that the candidate knows a comparison with `NULL` is never true, so `WHERE` excludes those rows.)
- How would you verify the baseline isn't inflated by duplicate `call_id` rows? (Tests data quality instincts: comparing `COUNT(*)` with `COUNT(DISTINCT call_id)` before trusting the extract.)
- This runs daily over 80M rows. How would you make the `latency` filter cheaper? (Tests layout knowledge: clustering or sorting files on `latency` so min/max statistics can skip data the `call_time` partitioning can't.)