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

api_tokens: 10 of 200 rows
token_idowner_idscopestatusissuedexpireslast_usedrequests
1043197writerevoked2025-02-022027-02-022026-02-0247
1086294adminexpired2025-03-032027-03-032026-03-0394
1129391read:usersActive2025-04-042027-04-042026-04-04141
1172488write:ordersREVOKED2025-05-052027-05-052026-05-05188
1215585read:analyticsactive2025-06-062027-06-062026-06-06235
1258682full_accessrevoked2025-07-07NULL2026-07-07282
1301779readexpired2025-08-082027-08-082026-08-08329
1344876writeActive2025-09-092027-09-09NULL0
1387973adminREVOKED2025-10-102027-10-102026-01-10423
14301070read:usersactive2025-11-112027-11-112026-02-11470

Expected output

First 5 of 50 rows
token_idowner_idscopestatusissuedexpireslast_usedrequestsrn
3150100writeactive2025-03-232027-03-232026-06-2323501
3193197adminrevoked2025-04-242027-04-242026-07-2423971
3236294read:usersexpired2025-05-252027-05-252026-08-2524441
3279391write:ordersActive2025-06-262027-06-262026-09-0224911
1172488write:ordersREVOKED2025-05-052027-05-052026-05-051881

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

`ROW_NUMBER` per owner with explicit tie-break
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_id

Common follow-up questions