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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Rolling Average with SQL Joins
00:00
5 left

Rolling Average with SQL Joins

MediumSQL · PostgreSQL

Problem

Write a SQL query to join two tables and calculate the rolling average of survey responses over a specific time window.

Use the provided survey questions and responses data. Restrict responses to January 1 through January 10, 2025, and calculate the rolling average over the current date and preceding six calendar days.

Output

  1. Return one row per matching response.
  2. Include response_id, question_text, survey_date, response_score, and rolling_avg_7_days.
  3. Order by question_id, survey_date, and response_id ascending.

Output details

  • Calculate rolling averages independently for each question_id.
  • Round rolling_avg_7_days to two decimal places.
  • Exclude NULL response_score values from averages while retaining their response rows.
  • Include responses dated January 1 through January 10, 2025, inclusive.

Schema

survey_questions
ColumnTypeDescription
question_idPKINTUnique survey question identifier
question_textVARCHAR(255)Text presented to survey respondents
topicVARCHAR(100)Survey topic
survey_responses
ColumnTypeDescription
response_idPKINTUnique response identifier
question_idINTReferenced survey question
survey_dateDATEDate the response was recorded
response_scoreNUMERIC(4,2)Numeric survey response score
Tablessurvey_questionssurvey_responses
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.