Top Lessons Each Month
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
- L5
- Asked in
- Walmart
Problem
Walking the calendar from the earliest month forward, surface the 3 most-completed lessons in each month, most completions first, keeping every lesson that finishes level with one of them. The content team counts every logged row toward a lesson when deciding where to invest next quarter.
3 lessons a month earn the budget, and no equal gets left behind.
Tables
page_views
Columns: view_id INTEGER, page_url TEXT, user_id INTEGER, referrer TEXT, dur_ms INTEGER, device TEXT, viewed_at TIMESTAMP
| view_id | page_url | user_id | referrer | dur_ms | device | viewed_at |
|---|---|---|---|---|---|---|
| 5053 | /products | 1555 | amazon.com | 637 | desktop | 2026-09-02 01:09:00 |
| 5106 | /checkout | 1652 | twitter.com | 774 | tablet | 2026-09-03 02:18:00 |
| 5159 | /profile | 1749 | github.com | 911 | Mobile | 2026-09-04 03:27:00 |
| 5212 | /search | 1846 | NULL | 1048 | DESKTOP | 2026-09-05 04:36:00 |
| 5265 | /about | 1943 | reddit.com | 1185 | mobile | 2026-09-06 05:45:00 |
| 5318 | /blog/intro | 1555 | NULL | NULL | desktop | 2026-09-07 06:54:00 |
| 5371 | /pricing | 1652 | bing.com | 1459 | tablet | 2026-09-08 07:03:00 |
| 5424 | /docs/api | 1749 | news.ycombinator.com | 1596 | Mobile | 2026-09-09 08:12:00 |
| 5477 | /settings | 1846 | NULL | 1733 | DESKTOP | 2026-09-10 09:21:00 |
| 5530 | /Home | 1943 | bing.com | 1870 | mobile | 2026-09-11 10:30:00 |
Expected output
| month | page_url | completion_count | rank |
|---|---|---|---|
| 2022-01 | /about | 1 | 1 |
| 2022-01 | /products | 1 | 1 |
| 2022-02 | /checkout | 1 | 1 |
| 2022-02 | new_editor | 1 | 1 |
| 2026-01 | /checkout | 2 | 1 |
Worked solution and explanation
Why this problem exists in real interviews
This is per-group top-N with ties, dressed up as a content report. Almost everyone buckets the month and counts the rows correctly. What separates candidates is the ranking. RANK() partitioned by month keeps every lesson that finished level, while ROW_NUMBER() silently crowns one winner among equals. This log is full of ties, so the wrong function quietly deletes lessons the content team should be funding.
Notice the gap: the prompt talks about lesson completions, but the table is page_views, where a row is a view. In a real review you would push for a lesson_completions event source. Here page_url stands in for the lesson and every row counts as a completion, and the query bakes in that simplification.
Break down the requirements
Step 1: Bucket views into year-months
strftime('%Y-%m', viewed_at) yields a sortable label that keeps January 2022 apart from January 2026. Aggregating to (month, page_url) with COUNT(*) collapses the log into one row per lesson per month, with repeat views by the same learner all counted.
Step 2: Rank within each month
RANK() OVER (PARTITION BY month ORDER BY completion_count DESC) restarts every month. If 2 lessons tie for second, both get 2 and the next lesson gets 4.
Step 3: Keep the top 3 and order for the reader
WHERE rnk <= 3 runs after the window has assigned positions, so a lesson tied for third survives. The outer ORDER BY month, completion_count DESC gives the content team its calendar walk, and lessons level on both keys read correctly in either order.
The solution
WITH monthly AS (
SELECT strftime('%Y-%m', viewed_at) AS month,
page_url,
COUNT(*) AS completion_count
FROM page_views
GROUP BY 1, 2
),
ranked AS (
SELECT month,
page_url,
completion_count,
RANK() OVER (PARTITION BY month ORDER BY completion_count DESC) AS rnk
FROM monthly
)
SELECT month, page_url, completion_count, rnk AS "rank"
FROM ranked
WHERE rnk <= 3
ORDER BY month, completion_count DESCCommon follow-up questions
Common follow-up questions
- How would you change the query to count only views where `dur_ms` exceeds a completion threshold? (Tests moving from a proxy metric to a defensible engagement filter.)
- If the content team only cares about the last 6 months, what changes in the query plan? (Probes partition pruning on `viewed_at` and whether the predicate belongs inside the `monthly` CTE.)
- How would you return exactly 3 rows per month even on ties? (Forces the `RANK` vs `ROW_NUMBER` tradeoff and a discussion of fair tiebreakers.)