Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Rolling 7-Day Impressions and Clicks
00:00
5 left

Rolling 7-Day Impressions and Clicks

MediumSQL · PostgreSQL

Problem

Write a SQL query using window functions to compute a rolling 7-day average of impressions and clicks by publisher.

Use the publisher_daily_metrics table, which contains one row per publisher per calendar date. Calculate averages over the current date and up to the six preceding dates.

Output

  1. One row per publisher and metric date.
  2. Columns: publisher_name, metric_date, rolling_7_day_avg_impressions, and rolling_7_day_avg_clicks.
  3. Include only dates with metric records, ordered by publisher_name and metric_date ascending.

The window covers the current metric date and the 6 calendar days before it; dates with no rows are simply absent from the window, and all rows for the same publisher and date share one window.

Schema

publishers
ColumnTypeDescription
publisher_idPKINTUnique publisher identifier
publisher_nameVARCHAR(100)Publisher display name
publisher_daily_metrics
ColumnTypeDescription
metric_idPKINTUnique daily metric record identifier
publisher_idINTPublisher associated with the daily metrics
metric_dateDATECalendar date of the metrics
impressionsNUMERIC(12,2)Daily ad impressions
clicksNUMERIC(12,2)Daily ad clicks
Tablespublisherspublisher_daily_metrics
Interviewer

Your question is Rolling 7-Day Impressions and Clicks. Start with the requirements and the two tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.