The Quiet Outlier

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4

Problem

Surface the largest latency outlier in api_calls, judging each exact latency value by how many calls logged it. A value logged only once could be a measurement glitch and one logged three or more times is ordinary traffic; the outliers are the values in between.

Ignore what the traffic does all day. Find the spike that barely showed up.

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
largest_outlier_latency
241.3

Worked solution and explanation

What this really asks

This is a frequency filter wearing an SRE badge. Each exact latency value is judged by how many calls logged it, only the values logged exactly twice count as outliers, and the answer is the largest of those. Anyone can write MAX(latency). What separates candidates is pinning the count at the value grain and reading the band correctly: a value logged once is a possible glitch, not an outlier. Get that wrong and you hand back 1713.7, a one-off reading the definition throws out, instead of 241.3.

The seed makes the trap bite. The top of the latency range is a long tail of one-offs: 1713.7, 1696.4 and their neighbours were each logged a single time. Keep singletons and they win every time, which is why a bare MAX(latency) and HAVING COUNT(*) <= 2 land on the same wrong number.

Step 1: Collapse to one row per latency value

GROUP BY latency turns the call log into one row per exact reading, and COUNT(*) inside each group is the number of calls that hit that value. This grain shift is the whole problem: frequency belongs to the value, not to any single call, so no row-level filter can see it.

Step 2: Keep only values logged exactly twice

HAVING COUNT(*) = 2 is the band the statement describes: more than once rules out the glitch, fewer than three rules out normal traffic. Watch 248.4: it looks twice-seen inside the February traffic, but a third call logged it earlier, so it drops out. Park the survivors in a CTE called twice_seen.

Step 3: Return the largest survivor as one value

Read the answer back with a scalar subquery, (SELECT MAX(latency) FROM twice_seen). It yields exactly one row and one column, so it is the single metric the question wants. The six calls with no latency form their own group of six and never pass the filter, and MAX would ignore them anyway.

Largest latency logged exactly twice
WITH twice_seen AS (
  SELECT latency
  FROM api_calls
  GROUP BY latency
  HAVING COUNT(*) = 2
)
SELECT (
  SELECT MAX(latency)
  FROM twice_seen
) AS largest_outlier_latency

Count per value in the CTE, keep the twice-seen values, then read the extreme back as one scalar.

Reads outlier as rare
HAVING COUNT(*) <= 2, or no count at all and a straight MAX(latency). Both return 1713.7, a reading logged once.
Reads the band exactly
HAVING COUNT(*) = 2 in the twice_seen CTE, then MAX(latency) over the survivors. Returns 241.3, the largest value two calls agree on.

Common follow-up questions