Who Moved the Needle

A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
medium
Seniority
L4
Asked in
Spotify

Problem

We're compiling a leaderboard for the onboarding_v3 experiment, where a user's standing is the total metric value they drove. Return the users in the top ten standings, biggest first, along with the variant each was assigned.

In every experiment, a handful of users carry the result. Find them.

Tables

ab_results

Columns: result_id INTEGER, test_name TEXT, variant TEXT, user_id INTEGER, metric TEXT, value REAL, created TEXT

ab_results: 10 of 200 rows
result_idtest_namevariantuser_idmetricvaluecreated
237new_homepagetreatment_a100click_rate13.72026-02-02
274pricing_pagetreatment_b197revenue27.42026-03-03
311onboarding_v3Control294bounce41.12026-04-04
348search_algo_bCONTROL391time_on_page54.82026-05-05
385rec_engine_v2control488signup68.52026-06-06
422checkout_flow_v2treatment_a585conv_rate82.22026-07-07
459new_homepagetreatment_b682click_rate95.92026-08-08
496pricing_pageControl779revenue9.62026-09-09
533onboarding_v3CONTROL876bounce23.32026-01-10
570search_algo_bcontrol973time_on_pageNULL2026-02-11

Expected output

First 5 of 10 rows
rnkvariantuser_idtotal_value
1treatment_a4950197.4
2treatment_b8442183.8
3treatment_a2040175.4
5Control9024148.2
8CONTROL9606112.6

Worked solution and explanation

What this really is

Beneath the leaderboard costume this is a top-N-with-ties problem, and it is decided almost entirely by which ranking function you reach for. When two users tie on total value near the cutoff, the real question is whether your 'top ten' keeps both of them and whether the genuine eleventh-best user still lands at rank eleven instead of getting bumped off the board. Reach for RANK and a pair of ties at the top leaves a gap that shoves a real top-ten standing out of the results. Reach for ROW_NUMBER and you return exactly ten tidy rows but break the tie on a coin flip, hiding the fact that two users are actually level. DENSE_RANK is the only one that keeps tied users together and keeps the ranks contiguous. The second, quieter trap: you must total each user's measurements into one number before you rank, or the window ranks raw rows and the same user surfaces twice.


Reading the requirements

Step 1: Scope to the one test

Filter WHERE test_name = 'onboarding_v3' first. The table holds millions of rows across thousands of experiments, so this filter is the single largest cost reducer in the plan, and it has to happen before any aggregation or ranking or you rank the wrong population entirely.

Step 2: Total each user before ranking

One user can log several measurements inside the test. GROUP BY variant, user_id with SUM(value) gives one total per user, and ranking by SUM(value) rather than raw value is what stops a user from landing on the board more than once. SUM quietly ignores NULL measurements, which matches the intent here.

Step 3: DENSE_RANK, then filter in an outer query

DENSE_RANK() OVER (ORDER BY SUM(value) DESC) keeps tied users on the same rank with no gaps. Because window functions are illegal in WHERE, wrap the ranked query in a subquery and filter rnk <= 10 on the outside. That can return more than ten rows when users tie, which is precisely what 'top ten standings' means.


The solution

Scope, total, `DENSE_RANK`, then slice
SELECT rnk, variant, user_id, total_value
FROM (
  SELECT variant, user_id, SUM(value) AS total_value,
    DENSE_RANK() OVER (ORDER BY SUM(value) DESC) AS rnk
  FROM ab_results
  WHERE test_name = 'onboarding_v3'
  GROUP BY variant, user_id
)
WHERE rnk <= 10
RANK at a top-3 cutoff
Two users tie for first, so ranks come out 1, 1, 3, 4. Filtering rank <= 3 keeps the two firsts and the single third, and the fourth row is gone even though only three distinct standings exist. The gap ate a real result.
`DENSE_RANK` at a top-3 cutoff
The same tie comes out 1, 1, 2, 3. Filtering rnk <= 3 keeps three distinct standings including that fourth row, so no genuine top-three performer is lost to a gap.

Common follow-up questions