User Age Ranking
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
- Airbnb
Problem
Assign each user a rank based on their age bucket in descending order (oldest bucket first). Show each user's ID and their assigned rank.
Age brackets, stacked from top to bottom.
Tables
users
Columns: user_id INTEGER, username TEXT, email TEXT, signup_date TEXT, account_status TEXT, age_bucket TEXT
| user_id | username | signup_date | account_status | age_bucket | |
|---|---|---|---|---|---|
| 100 | alice | alice@example.com | 2025-02-02 | inactive | 25-34 |
| 197 | aaron42 | aaron42@example.com | 2026-03-03 | suspended | 35-44 |
| 294 | amelia | amelia@example.com | 2024-04-04 | pending_verification | 45-54 |
| 391 | arjun | arjun@example.com | 2025-05-05 | active | 55-64 |
| 488 | ava99 | ava99@example.com | 2026-06-06 | inactive | 65+ |
| 585 | andrew_k | andrew_k@example.com | 2024-07-07 | suspended | NULL |
| 682 | anika | anika@example.com | 2025-08-08 16:34:00 | pending_verification | 18-24 |
| 779 | aiden | aiden@example.com | 2026-09-09 14:53:00 | active | 25-34 |
| 876 | aria_b | aria_b@example.com | 2024-10-10 | inactive | 35-44 |
| 973 | brian | brian@example.com | 2025-11-11 | suspended | 45-54 |
Expected output
| user_id | age_rank |
|---|---|
| 488 | 1 |
| 1167 | 1 |
| 391 | 2 |
| 294 | 3 |
| 197 | 4 |
Worked solution and explanation
A rank over 7 positions
This is a dense ranking of a tiny dimension dressed up as 'assign each user a rank'. Writing the window is easy. The trap is the phrase itself: it sounds like every user gets their own number, so people reach for ROW_NUMBER(). But age_bucket holds only 6 real labels plus a missing value, so the 200 users collapse onto 7 positions and everyone in a bucket shares one. ROW_NUMBER() splits users 488 and 1167 into 1 and 2. RANK() ties them, then skips to 30 for '55-64' because 29 users sit in '65+'. Only DENSE_RANK() ties without gaps.
Step 1: Rank over the bucket, never the row
Users with equal buckets are peers in the window, so a ranking that respects ties gives them one shared number. DENSE_RANK() then hands the next distinct bucket the very next integer, which is the 1, 1, 2, 3 the preview shows.
Step 2: Trust the text sort for oldest first
The labels '18-24', '25-34' through '65+' are text, but their leading digit rises with age, so ORDER BY age_bucket DESC puts '65+' first and '18-24' last. No parsing of the ranges is needed on this data.
Step 3: Let missing buckets take the last position
28 users have a NULL age_bucket, and they still belong in the output. The grader sorts NULL below every value, so under DESC those users land at the end and share the final rank 7. Filtering them out would drop rows the answer expects.
SELECT user_id,
DENSE_RANK() OVER (ORDER BY age_bucket DESC) AS age_rank
FROM users
ORDER BY age_rank, user_id;The alias is age_rank because rank is reserved in some dialects.
- Per-row numbering
ROW_NUMBER()gives 488 and 1167 the values 1 and 2.RANK()ties them at 1 but gives '55-64' rank 30, so every later bucket is off.- Shared, gapless rank
DENSE_RANK()gives both '65+' users rank 1 and '55-64' rank 2. The highest rank equals the number of distinct buckets,NULLincluded: 7.
SELECT user_id,
ROW_NUMBER() OVER (ORDER BY age_bucket DESC) AS age_rank
FROM users;Returns 1 to 200, one number per user, which the preview rules out.
Common follow-up questions
- What if `age_bucket` labels stopped sorting correctly as text? (Extract the lower bound with `SUBSTR` or a regex and `CAST` it to an integer, then order the window by that.)
- How would you rank within each `account_status` group? (Add `PARTITION BY account_status` to the window so the ranking restarts in each group.)
- How would you avoid sorting every user just to rank 7 values? (Rank the distinct buckets in a small CTE and join it back to `users` on `age_bucket`, handling the `NULL` bucket explicitly.)