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

user_sessions: 10 of 200 rows
session_iduser_iddevice_idsession_startsession_duration_secpages_viewed
70611972302026-02-02 01:00:00533
71222942592026-03-03 02:00:00766
71833912882026-04-04 03:00:00999
72444883172026-05-05 04:00:0012212
73055853462026-06-06 05:00:0014515
73666823752026-07-07 06:00:0016818
74277794042026-08-08 07:00:0019121
7488876NULL2026-09-09 08:00:0021424
75499734622026-01-10 09:00:0023727
761010704912026-02-11 10:00:0026030

Expected output

First 5 of 20 rows
user_idtotal_sessions
1008
1975
2945
3915
4885

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

Group, then filter against the average of counts
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_id
The tempting version that averages the wrong thing
SELECT 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_id

AVG(session_id) averages identifiers across every session row. The cutoff lands in the thousands, so no user clears it.


Common follow-up questions