The Loudest Caller
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
- L4
- Asked in
- Amazon
Problem
One account's tokens in api_tokens carry more total request volume than any other account's, and we want to know which parts of the API it never touches. List the scopes that appear on our tokens but on none of the busiest account's tokens; if several accounts tie for the top total, leave out the scopes of all of them.
One account carries the traffic. Trace it back to everything it can touch.
Tables
api_tokens
Columns: token_id INTEGER, owner_id INTEGER, scope TEXT, status TEXT, issued TEXT, expires TEXT, last_used TEXT, requests INTEGER
| token_id | owner_id | scope | status | issued | expires | last_used | requests |
|---|---|---|---|---|---|---|---|
| 1043 | 197 | write | revoked | 2025-02-02 | 2027-02-02 | 2026-02-02 | 47 |
| 1086 | 294 | admin | expired | 2025-03-03 | 2027-03-03 | 2026-03-03 | 94 |
| 1129 | 391 | read:users | Active | 2025-04-04 | 2027-04-04 | 2026-04-04 | 141 |
| 1172 | 488 | write:orders | REVOKED | 2025-05-05 | 2027-05-05 | 2026-05-05 | 188 |
| 1215 | 585 | read:analytics | active | 2025-06-06 | 2027-06-06 | 2026-06-06 | 235 |
| 1258 | 682 | full_access | revoked | 2025-07-07 | NULL | 2026-07-07 | 282 |
| 1301 | 779 | read | expired | 2025-08-08 | 2027-08-08 | 2026-08-08 | 329 |
| 1344 | 876 | write | Active | 2025-09-09 | 2027-09-09 | NULL | 0 |
| 1387 | 973 | admin | REVOKED | 2025-10-10 | 2027-10-10 | 2026-01-10 | 423 |
| 1430 | 1070 | read:users | active | 2025-11-11 | 2027-11-11 | 2026-02-11 | 470 |
Expected output
| scope |
|---|
| read:users |
| write:orders |
| read:analytics |
| full_access |
| read |
Worked solution and explanation
What this problem really is
This is a filter-to-the-argmax feeding a set difference, dressed up as an access review. Anyone can total requests per account. The trap is that the winner is an account, not a token: one owner holds several tokens, and a tie can crown more than one owner. Sort raw tokens by requests and grab the top one, and you subtract a single token's scope instead of everything the busiest account holds. The scopes it really does reach then leak into your answer as 'never touched', which is exactly the wrong thing to hand a security review.
Three hops, in order
Step 1: Total requests per account
Group api_tokens on owner_id and add up requests. The 'busiest account' metric lives at the owner grain; any comparison against a single token's requests is comparing apples to orchards.
Step 2: Keep every owner at the top
MAX over those per-owner totals gives you one number with no owner attached. Feeding it back into HAVING SUM(requests) = (...) returns every owner who hit it, so a tie at the top quietly brings in both accounts.
Step 3: Subtract the scopes they hold
Collect every scope on those owners' tokens, then return the DISTINCT scopes in api_tokens that are NOT IN that set. DISTINCT matters because each surviving scope sits on thousands of tokens.
The solution
SELECT DISTINCT scope
FROM api_tokens
WHERE scope NOT IN (
SELECT scope
FROM api_tokens
WHERE owner_id IN (
SELECT owner_id
FROM api_tokens
GROUP BY owner_id
HAVING SUM(requests) = (
SELECT MAX(total_req)
FROM (
SELECT SUM(requests) AS total_req
FROM api_tokens
GROUP BY owner_id
) AS owner_totals
)
)
)Innermost out: per-owner totals, their maximum, the owners at it, their scopes, then everything else.
Common follow-up questions
- If two accounts tie for the highest total, which scopes should be excluded? (Tests tie handling: matching on the max total keeps every tied owner, which a single-row limit would miss.)
- What changes if `scope` can be NULL on some tokens? (Tests NULL semantics of `NOT IN` versus `NOT EXISTS`.)
- How would you limit both sides of the comparison to tokens whose `status` is 'active'? (Tests whether the candidate can restrict the base set without breaking the argmax.)