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

page_views: 10 of 200 rows
view_idpage_urluser_idreferrerdur_msdeviceviewed_at
5053/products1555amazon.com637desktop2026-09-02 01:09:00
5106/checkout1652twitter.com774tablet2026-09-03 02:18:00
5159/profile1749github.com911Mobile2026-09-04 03:27:00
5212/search1846NULL1048DESKTOP2026-09-05 04:36:00
5265/about1943reddit.com1185mobile2026-09-06 05:45:00
5318/blog/intro1555NULLNULLdesktop2026-09-07 06:54:00
5371/pricing1652bing.com1459tablet2026-09-08 07:03:00
5424/docs/api1749news.ycombinator.com1596Mobile2026-09-09 08:12:00
5477/settings1846NULL1733DESKTOP2026-09-10 09:21:00
5530/Home1943bing.com1870mobile2026-09-11 10:30:00

Expected output

First 5 of 142 rows
monthpage_urlcompletion_countrank
2022-01/about11
2022-01/products11
2022-02/checkout11
2022-02new_editor11
2026-01/checkout21

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

Top lessons each month
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 DESC

Common follow-up questions

Common follow-up questions