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

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 78 rows
call_idendpointmethodstatuslatencyuser_idcall_timeerr_msg
16000/api/v1/usersGET200149502023-01-01 00:00:00NULL
8079/api/v1/ordersPOST2004.21002026-02-02 01:07:00NULL
8158/api/v2/productsPUT2007.91972026-03-03 02:14:00NULL
21000/api/v1/usersGET2001021372026-02-01 00:00:00NULL
8474/api/v2/analyticspost40022.75852026-07-07 06:42:00Not 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

Inclusive threshold filter for the baseline
SELECT
    call_id,
    endpoint,
    method,
    status,
    latency,
    user_id,
    call_time,
    err_msg
FROM api_calls
WHERE latency <= 50
ORDER BY latency ASC

One scan, one inclusive predicate, one sort on latency.

Strict cutoff
WHERE latency < 50 drops 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 <= 50 keeps the boundary call, matching the definition the team wrote: exactly 50 still counts.
The version that loses the boundary
SELECT
    call_id,
    endpoint,
    method,
    status,
    latency,
    user_id,
    call_time,
    err_msg
FROM api_calls
WHERE latency < 50
ORDER BY latency ASC

One character different, and the 50 ms call is gone.


Common follow-up questions