The Tiebreaker
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
- Oracle
Problem
Our admin directory shows every account in users by its user id, email, signup date, account status and age bracket, with the status groups in alphabetical order. Within each status the oldest bracket comes first and accounts with no recorded bracket close out the group.
One column wasn't enough. The second column settles it.
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 | signup_date | account_status | age_bucket | |
|---|---|---|---|---|
| 1167 | beatrice@example.com | 2024-01-13 | active | 65+ |
| 3883 | felix@example.com | 2025-05-13 | active | 65+ |
| 6599 | kira@example.com | 2026-06-13 | active | 65+ |
| 488 | ava99@example.com | 2026-06-06 | inactive | 65+ |
| 3204 | NULL | 2024-10-06 | inactive | 65+ |
Worked solution and explanation
What this one is really asking
This is a multi-level ordering dressed up as an admin directory, and the levels do not share a direction. Anyone can sort by account_status. The catch is that four statuses cover the whole table, so status alone leaves every group in whatever order the engine returns. Write one direction for every key and you flip a level: the directory reads youngest first, or the status groups run backwards.
Step 1: Return the directory fields
The directory shows five fields, so the query names user_id, email, signup_date, account_status and age_bucket with no filter. Every account appears, including the 28 with no age_bucket.
Step 2: Make account_status the outer level
The groups run alphabetically, so the first key is account_status ASC. Position in the clause sets priority: this key decides first, and later keys only matter where it ties.
Step 3: Give age_bucket its own direction
Inside a group the oldest bracket leads, so the second key is age_bucket DESC. A direction keyword binds only to the column right before it, which is why one clause can mix ASC and DESC. NULLS LAST pins the unknown brackets to the end of their group.
Step 4: Settle the last ties with user_id
A status and a bracket together still cover several accounts: 'active' with no bracket covers six. user_id ASC makes the sequence a function of the data, so two correct answers can never disagree on order.
SELECT
user_id,
email,
signup_date,
account_status,
age_bucket
FROM users
ORDER BY
account_status ASC,
age_bucket DESC NULLS LASTGroups A to Z, brackets oldest to youngest inside each, unknown brackets last, then lowest user_id.
- One direction everywhere
ORDER BY account_status, age_bucket, user_idopens every group with the unknown brackets and '18-24'. Making every keyDESCfixes the brackets but puts 'suspended' ahead of 'active'.- One direction per key
account_status ASC, age_bucket DESC NULLS LAST, user_id ASCgives each key the direction the directory needs, so the groups and the sequence inside them are both right.
Common follow-up questions
- How would you page through this directory efficiently? (Because the order ends on `user_id`, it is total, so keyset pagination on the three keys works instead of a growing `OFFSET`.)
- What changes if a '5-17' bracket is added to `age_bucket`? (Text ordering no longer matches age order. Checks whether you would map brackets to a number.)
- What would this query return on Postgres without `NULLS LAST`? (Tests knowing each engine's default placement of `NULL` under `DESC`.)