Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Window Functions and Rolling Avg

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

Your question is SQL Window Functions and Rolling Avg. Start with the requirements and the three 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

ABC Family wants to monitor daily viewing trends in its streaming app. Write a PostgreSQL query that calculates a three-calendar-day rolling average of completed viewing minutes for each active ABC Family show.

Requirements

  1. Include every date from January 1 through January 5, including dates with no qualifying viewing sessions.
  2. Count only completed sessions from users whose country is US.
  3. Aggregate viewing minutes by show and date, treating missing or NULL minutes as zero.
  4. Calculate the rolling average for the current date and the previous two calendar dates, restarting the window for each show.
  5. Return active ABC Family shows ordered by show title and report date.

Schema

shows
ColumnTypeDescription
show_idPKINTUnique show identifier
show_titleVARCHAR(100)Show title
networkVARCHAR(50)Network or platform owner
genreVARCHAR(50)Show genre
is_activeBOOLEANWhether the show is currently active
users
ColumnTypeDescription
user_idPKINTUnique user identifier
countryVARCHAR(2)Two-letter user country code
viewing_sessions
ColumnTypeDescription
session_idPKINTUnique viewing session identifier
user_idINTUser who watched the show
show_idINTShow being watched
started_atDATECalendar date when viewing started
duration_minutesINTMinutes watched in the session
session_statusVARCHAR(20)Session completion status
Tablesshowsusersviewing_sessions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results