Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

7-Day Rolling Active Learners

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is 7-Day Rolling Active Learners. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Khan Academy wants to monitor learner engagement across grade levels. Write a PostgreSQL query that calculates the daily number of distinct active learners for each grade level and a seven-day rolling average of that count from January 1 through January 10, 2025.

Requirements

  1. Generate one output row for every date and grade level, including dates with no activity.
  2. Count each learner at most once per grade level per day.
  3. Calculate the rolling average using the current day and the six preceding calendar days. Round the result to two decimal places.
  4. Return rows ordered by grade_level and activity_date.

Schema

learners
ColumnTypeDescription
learner_idPKINTEGERUnique learner identifier
learner_nameVARCHAR(100)Learner display name
grade_levelVARCHAR(20)Learner's grade level
learner_activity
ColumnTypeDescription
activity_idPKINTEGERUnique activity event identifier
learner_idINTEGERLearner associated with the activity event
activity_dateDATEDate of the activity
activity_typeVARCHAR(50)Type of Khan Academy activity
Tableslearnerslearner_activity
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results