cswork / libraryOpen practice workspace

Coding questions · Meta

Resolve Ties for Top-10 Users in SQL Query

This question evaluates proficiency in SQL ranking and tie-resolution techniques for generating deterministic top-N leaderboards, focusing on handling identical scores while producing an exact set of rows.

Oculus_Scores

+---------+-------+
| user_id | score |
+---------+-------+
| u1      | 95    |
| u2      | 92    |
| u3      | 90    |
| u4      | 90    |
| u5      | 88    |
+---------+-------+
Scenario

Meta DSPA onsite – SQL screen on Oculus dataset; need top-10 leaderboard when ranks 10 and 11 are tied.

Question

Table Oculus_Scores(user_id, score) holds daily user scores. Write a SQL query that returns exactly the top-10 users by score even when users ranked 10 and 11 have identical scores. Explain your logic.

Hints

Use DENSE_RANK vs ROW_NUMBER; trim with LIMIT or sub-query so only first 10 rows are emitted after ordering.

Top-10 leaderboard with tied scores at cutoff

Table Oculus_Scores(user_id, score) holds daily user scores. Write a SQL query that returns exactly the top 10 users by score, ordered from highest to lowest. When users ranked 10 and 11 have the same score, your query must still return only 10 users by applying a deterministic tie-breaker (for example, user_id). If fewer than 10 users exist, return all of them.

Table Oculus_Scores

columntypeconstraints
user_idVARCHARPRIMARY KEY, NOT NULL
scoreINTEGERNOT NULL

Sample data:

scoreuser_id
95u1
92u2
90u3
90u4
88u5

Expected output

Return up to 10 users ordered by highest score. If fewer than 10 users exist, return all of them. Break ties deterministically with a secondary sort key (e.g., user_id) so that when ranks 10 and 11 share the same score, only the first 10 (by the tie-breaker) are included. Using ROW_NUMBER ensures one row per position, unlike DENSE_RANK which could emit more than 10 rows when ties occur at the cutoff.

scoreuser_id
95u1
92u2
90u3
90u4
88u5

Hints

  1. Use a window function to assign an ordering position per user.
  2. Compare DENSE_RANK and ROW_NUMBER: which guarantees exactly 10 rows when there are ties at the cutoff?
  3. Apply a secondary ORDER BY key such as user_id to break ties deterministically, then filter to the first 10 positions.