Between Two Extremes
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
- L3
- Asked in
- Uber
Problem
Given a set of latency values, sum everything that falls strictly between the global minimum and maximum, excluding the extremes themselves. Return the min, the max, and the sum of everything in between.
Ignore the loudest and the quietest. The truth lives in the middle.
Tables
api_calls
Columns: call_id INTEGER, endpoint TEXT, method TEXT, status INTEGER, latency REAL, user_id INTEGER, call_time TEXT, 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
| min_latency | max_latency | sum_between |
|---|---|---|
| 1 | 1713.7 | 197499.4 |
Worked solution and explanation
Why this problem exists in real interviews
This tests whether a candidate can demonstrate writing subqueries that integrate with the outer query. This pattern appears frequently in mid-level SQL rounds where interviewers want to see structured thinking.
Break down the requirements
Step 1: Apply the range filter
The WHERE clause restricts rows to the target range. Applying this filter early reduces the volume flowing into downstream operations.
Step 2: Use a subquery to find the reference value
The scalar subquery computes a single value (like the maximum) that the outer query filters against. This avoids a self-join.
The solution
SELECT (SELECT MIN(latency) FROM api_calls) AS min_latency, (SELECT MAX(latency) FROM api_calls) AS max_latency, SUM(latency) AS sum_between
FROM api_calls
WHERE latency > (SELECT MIN(latency) FROM api_calls) AND latency < (SELECT MAX(latency) FROM api_calls)Common follow-up questions
- If multiple rows share the exact minimum latency value, does your query exclude all of them or only one? (Tests understanding of value-based exclusion; all rows at the min or max value should be excluded.)
- What does the sum return if all latency values are identical, making min equal to max? (Tests the degenerate case where nothing falls strictly between equal bounds, yielding NULL or zero.)
- Could you solve this in a single pass without a subquery using window functions, and what are the trade-offs? (Tests ability to use MIN/MAX as window aggregates and awareness of full-table scan cost.)