Streamhub's engagement analyst ranks sessions by their event count — sessions with the same event count share a rank, with no positions skipped.
Write a query to return the ID and event count of every session, plus the session's dense rank by event_count in descending order.
Assumptions:
- The
sessionstable has one row per session with anidand anevent_count. - Rank
1goes to sessions with the highestevent_count, with rank increasing asevent_countdecreases. - Sessions with the same
event_countshare a rank, and the next rank is always exactly one higher.
Output:
- One row per session, with columns
id,event_count, andengagement_rank.
Schema · analytics 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
id,
event_count,
DENSE_RANK() OVER (
ORDER BY
event_count DESC
) AS engagement_rank
FROM
sessions The shape
DENSE_RANK() OVER (ORDER BY event_count DESC) groups sessions into consecutive engagement tiers: every session that shares an event_count value gets the same tier number, and the next-distinct count gets the next consecutive integer. The output reads as a tier map, not a competitive ranking.
Clause by clause
SELECT id, event_count, DENSE_RANK() OVER (ORDER BY event_count DESC) AS engagement_rankreturns each session's ID, its event count, and its engagement tier. The window'sORDER BY event_count DESCsorts sessions from highest activity to lowest; withoutPARTITION BY, the entire sessions table is one window.DENSE_RANKthen walks the sorted sequence assigning1to the highest count,2to the next-distinct count, and so on.FROM sessionsreads every session. No filter; every session is placed in a tier.
Why DENSE_RANK and not RANK
The brief asks for engagement tiers, which is a categorical reading of the data. DENSE_RANK returns 1, 2, 3, ... consecutively across distinct values, so tier 1 is the highest engagement, tier 2 is the next-highest, and the integer is directly interpretable as "which band does this session sit in." RANK would skip ahead after ties, leaving gaps that mean nothing in a tier-counting context. When the reader is meant to count tiers ("there are five engagement bands"), DENSE_RANK is the correct function.
The trap
A session showing engagement_rank = 1 is not necessarily the single most active session; it shares its tier with every other session at the same event_count. DENSE_RANK deliberately collapses ties. If the question shifts to "which is the single most active session," DENSE_RANK is the wrong tool because it cannot distinguish ties at the top. The right answer there is ORDER BY event_count DESC LIMIT 1 or a tie-breaking secondary sort, not a ranking function.
You practiced DENSE_RANK() OVER (ORDER BY ... DESC) — gap-free ranking ordered descending; consecutive values stay continuous through ties.