Active Token Owners in 2026

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

Problem

The platform team is auditing the API access granted in 2026 and needs the size of that year's token-holder base. Find how many different owners were issued a token during the year.

Active token owners this year.

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 1 of 1 rows
distinct_owners
10

Worked solution and explanation

What this really is

Strip off the audit framing and this is a count of unique keys inside a one-year slice. The whole problem hinges on one word: owners, not tokens. Every owner in api_tokens holds several tokens, and the 2026 slice alone has 20 tokens spread across 10 owners, two apiece. Reach for COUNT(*) or COUNT(owner_id) and you count rows. The answer comes back as 20, double the real token-holder base, and nobody reviewing the query catches it until someone asks why the audit found twice as many people as the access list.

Step 1: Pin the window to issued

The year filter rides on issued, not on expires or last_used. A token issued in a prior year that is still valid this year does not belong to this year's grant cohort, and neither does a token used this year but issued earlier. strftime('%Y', issued) extracts the four-digit year as text, so compare it against the year as a string literal, not a number.

Step 2: Decide the grain before you count

The ask is the size of the token-holder base, which is a count of people. That is the owner_id grain. Tokens are the rows; owners are what you report. Saying this out loud is the difference between a correct answer and a plausible-looking wrong one.

Step 3: Count the uniques

Apply COUNT(DISTINCT owner_id) over the filtered rows. The result is a single integer, so alias it clearly and return one row.

Unique owners issued a token this year
SELECT COUNT(DISTINCT owner_id) AS distinct_owners
FROM api_tokens
WHERE strftime('%Y', issued) = '2026'

One scan: filter to the year on issued, then dedup and count owners in a single aggregate.

Counts rows (wrong)
COUNT(owner_id) returns the number of tokens issued this year: 20 here. With repeat holders it overshoots the headcount, and the bug is invisible because the number still looks reasonable.
Counts owners (correct)
COUNT(DISTINCT owner_id) returns the number of different people: 10 here, the token-holder base the audit asked for.

Common follow-up questions