Most Recent Token Usage
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
- OpenDoor
Problem
The security team is auditing API token activity. For each token owner, pull only the single most recently used record, working through owners from the lowest owner ID up to the highest. Include all token fields (ID, owner, scope, status, issued date, expiration, last used date, request count) and a row indicator.
Each user's latest token activity.
Tables
api_tokens
Columns: token_id INTEGER, owner_id INTEGER, scope TEXT, status TEXT, issued DATE, expires DATE, last_used DATE, 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
| token_id | owner_id | scope | status | issued | expires | last_used | requests | rn |
|---|---|---|---|---|---|---|---|---|
| 3150 | 100 | write | active | 2025-03-23 | 2027-03-23 | 2026-06-23 | 2350 | 1 |
| 3193 | 197 | admin | revoked | 2025-04-24 | 2027-04-24 | 2026-07-24 | 2397 | 1 |
| 3236 | 294 | read:users | expired | 2025-05-25 | 2027-05-25 | 2026-08-25 | 2444 | 1 |
| 3279 | 391 | write:orders | Active | 2025-06-26 | 2027-06-26 | 2026-09-02 | 2491 | 1 |
| 1172 | 488 | write:orders | REVOKED | 2025-05-05 | 2027-05-05 | 2026-05-05 | 188 | 1 |
Worked solution and explanation
What this really is
This is a greatest-per-group pick dressed up as a security audit. Anyone can find each owner's latest date with GROUP BY owner_id and MAX(last_used). The audit wants the whole winning token, though (its scope, status, expiry and request count), and an aggregate hands back a date, not a row. Patch that with a join back on last_used and owners with tied dates show up twice. An owner whose tokens were never used vanishes, because a NULL never equals anything.
Build it in 3 moves
Step 1: Number each owner's tokens, latest first
ROW_NUMBER() with PARTITION BY owner_id and ordered by last_used DESC restarts the count at 1 for every owner and gives that 1 to the newest token. It never hands out the same number twice, so exactly one row per owner wins.
Step 2: Break ties on the larger token_id
Ordering by last_used alone leaves the winner up to the engine whenever 2 tokens share a date. Token IDs are issued in sequence, so adding token_id DESC as the second key picks the more recent token and returns the same answer on every run.
Step 3: Filter rn = 1 outside, then sort by owner_id
A window function is computed after WHERE, so the filter on rn has to live in an outer query. The inner SELECT * carries all 8 token columns through, rn rides along as the row indicator, and the outer ORDER BY owner_id gives the audit its lowest-to-highest order.
The solution
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY owner_id ORDER BY last_used DESC, token_id DESC) AS rn
FROM api_tokens
)
WHERE rn = 1
ORDER BY owner_idCommon follow-up questions
- How would you write this without a window function? (A per-owner `MAX(last_used)` joined back on `owner_id` and `last_used`, then a second step to break ties on `token_id`. It takes more code and 2 passes over the table, and NULL dates still need handling.)
- How would you return the latest 2 tokens per owner instead of one? (Change the filter to `rn <= 2`. The `token_id` tie-break matters more now, since a tie can decide which token makes the cut.)
- What if the audit wanted never-used tokens excluded entirely? (Filter `last_used IS NOT NULL` in the inner query, before numbering. Owners with no used token then disappear, so confirm with the security team that this is acceptable.)