Active API Tokens

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
L3
Asked in
City and County of San Francisco

Problem

A token that was issued but never called has no last-used time recorded at all. An engineer on the developer platform team is auditing token usage and wants every field of each token in api_tokens that has actually been used.

Tokens that have actually been used.

Tables

api_tokens

Columns: token_id INTEGER, owner_id INTEGER, scope TEXT, status TEXT, issued TEXT, expires TEXT, last_used TEXT, 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 176 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

Worked solution and explanation

This is a missing-value test dressed up as a usage audit. 24 of the 200 tokens in api_tokens were issued and never called, and the only trace of that is an empty last_used. Anyone can write SELECT * FROM api_tokens WHERE .... What separates candidates is the predicate. Write last_used <> NULL and every comparison comes back unknown, the filter keeps nothing, and the audit reports zero used tokens when 176 have been used.

Reading the requirement

Step 1: Return every field

The audit wants each token row as stored, from token_id through requests, so SELECT * is the honest projection here. Listing eight columns by hand reads the same and only adds a place to drop one.

Step 2: Define used as a recorded last_used

The table never says 'used' outright. A never-called token has no last_used at all, and its requests sits at 0. The statement names the last-used time, so the presence of that value is the whole filter.

Step 3: Test presence, not equality

IS NOT NULL asks whether a value exists. last_used <> NULL asks whether the value differs from an unknown, and the answer is always unknown, which WHERE treats as false.

The solution

Every token with a recorded `last_used`
SELECT *
FROM api_tokens
WHERE last_used IS NOT NULL
Comparing to NULL
WHERE last_used <> NULL evaluates to unknown on all 200 rows. WHERE keeps only true, so the result is empty and the audit says no token was ever used.
Testing for NULL
WHERE last_used IS NOT NULL is true on the 176 rows with a timestamp and false on the 24 without one. Every used token comes back, and nothing else does.

Common follow-up questions