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

users: 10 of 200 rows
user_idusernameemailsignup_dateaccount_statusage_bucket
100alicealice@example.com2025-02-02inactive25-34
197aaron42aaron42@example.com2026-03-03suspended35-44
294 ameliaamelia@example.com2024-04-04pending_verification45-54
391arjun arjun@example.com2025-05-05active55-64
488ava99ava99@example.com2026-06-06inactive65+
585andrew_kandrew_k@example.com2024-07-07suspendedNULL
682anikaanika@example.com2025-08-08 16:34:00pending_verification18-24
779aidenaiden@example.com2026-09-09 14:53:00active25-34
876aria_baria_b@example.com2024-10-10inactive35-44
973brianbrian@example.com2025-11-11suspended45-54

Expected output

First 5 of 200 rows
user_idage_rank
4881
11671
3912
2943
1974

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.

Dense rank over a descending bucket window
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, NULL included: 7.
The tempting version
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