Above Average Interactions
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
- L5
- Asked in
- Samsara
Problem
A downstream report shows a small set of power users drives most of the platform's session volume. Pull every user whose total session count exceeds the average session count across all users, and lay them out as a leaderboard from the most sessions down to the fewest, breaking any tie in favor of the longer-standing account with the lower user ID.
The average user is boring. Who is above?
Tables
user_sessions
Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TIMESTAMP, session_duration_sec INTEGER, pages_viewed INTEGER
| session_id | user_id | device_id | session_start | session_duration_sec | pages_viewed |
|---|---|---|---|---|---|
| 7061 | 197 | 230 | 2026-02-02 01:00:00 | 53 | 3 |
| 7122 | 294 | 259 | 2026-03-03 02:00:00 | 76 | 6 |
| 7183 | 391 | 288 | 2026-04-04 03:00:00 | 99 | 9 |
| 7244 | 488 | 317 | 2026-05-05 04:00:00 | 122 | 12 |
| 7305 | 585 | 346 | 2026-06-06 05:00:00 | 145 | 15 |
| 7366 | 682 | 375 | 2026-07-07 06:00:00 | 168 | 18 |
| 7427 | 779 | 404 | 2026-08-08 07:00:00 | 191 | 21 |
| 7488 | 876 | NULL | 2026-09-09 08:00:00 | 214 | 24 |
| 7549 | 973 | 462 | 2026-01-10 09:00:00 | 237 | 27 |
| 7610 | 1070 | 491 | 2026-02-11 10:00:00 | 260 | 30 |
Expected output
| user_id | total_sessions |
|---|---|
| 100 | 8 |
| 197 | 5 |
| 294 | 5 |
| 391 | 5 |
| 488 | 5 |
Worked solution and explanation
What this problem really is
This is a two-level aggregation dressed up as a power-user report. The cutoff is the mean of the per-user session counts, an aggregate of an aggregate, and no column of user_sessions holds it. Everyone writes GROUP BY user_id correctly. Candidates split on where the benchmark comes from: average a per-session column such as session_id or pages_viewed and the cutoff lands nowhere near counts of 3 to 8, so the leaderboard comes back empty and the report names nobody.
Build it in 4 moves
Step 1: Collapse user_sessions to one row per user
GROUP BY user_id with COUNT(*) AS total_sessions folds repeated user_id rows into one total per user. That grain feeds both the filter and the benchmark.
Step 2: Average the counts in a derived table
The inner query repeats the GROUP BY user_id and names its count session_count. The outer SELECT AVG(session_count) then reduces those 59 rows to one number, about 3.39 on this data. Writing AVG(COUNT(*)) directly fails, because one aggregate cannot nest inside another.
Step 3: Filter groups with a strict > in HAVING
The comparison reads an aggregate, so it belongs in HAVING COUNT(*) > (...), which runs after grouping. A strict > keeps a user who sits exactly on the average off the power-user list.
Step 4: Order the leaderboard and break ties on user_id
ORDER BY total_sessions DESC, user_id puts the heaviest user first. 19 of the 20 power users hold 5 sessions each, so the second key decides almost the whole sequence: the lower ID, which is the older account, goes first.
The solution
SELECT user_id, COUNT(*) AS total_sessions
FROM user_sessions
GROUP BY user_id
HAVING COUNT(*) > (
SELECT AVG(session_count)
FROM (
SELECT COUNT(*) AS session_count
FROM user_sessions
GROUP BY user_id
) AS per_user
)
ORDER BY total_sessions DESC, user_idSELECT user_id, COUNT(*) AS total_sessions
FROM user_sessions
GROUP BY user_id
HAVING COUNT(*) > (SELECT AVG(session_id) FROM user_sessions)
ORDER BY total_sessions DESC, user_idAVG(session_id) averages identifiers across every session row. The cutoff lands in the thousands, so no user clears it.
Common follow-up questions
- How would you rewrite this with `AVG(...) OVER ()` instead of a scalar subquery? (Tests whether you can attach a global aggregate to every grouped row, and whether you know where the filter must then move.)
- Session counts are heavy-tailed. Would the median be a more useful threshold, and how would you compute it? (Tests outlier awareness, and whether you can compute the middle value of the per-user counts.)
- If the team only cared about the last 30 days, what changes about the plan and the result? (Tests partition pruning on a table partitioned by `session_start`, and the point that the average moves with the window.)
- How would you find users above the average for their signup-month cohort instead of the global average? (Tests turning a single global benchmark into one benchmark per cohort, joined back on the cohort key.)