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
Get started
Rolling 7-Day Messages Per Account
00:00
5 left

Rolling 7-Day Messages Per Account

HardSQL · PostgreSQL

Problem

Write a query to find the rolling 7-day average of messages sent per Twilio customer account, accounting for days with zero activity.

Use the provided customer account and message data for January 1 through January 10, 2024. Return every account for every date in that period, including accounts and dates with no messages.

Output

  1. One row per account and calendar date
  2. Columns: account_id, activity_date, message_count, and rolling_7_day_average
  3. The average should include available dates up to seven calendar days, with zero-message dates included
  4. Order by account_id, then activity_date

Output details

  • Round rolling_7_day_average to exactly 2 decimal places.
  • Define the seven-day window as the current date plus the six preceding calendar dates.
  • Sort by account_id ascending, then activity_date ascending.

Schema

customer_accounts
ColumnTypeDescription
account_idPKINTUnique Twilio customer account identifier
account_nameVARCHAR(100)Customer account name
created_atDATEDate the account was created
messages
ColumnTypeDescription
message_idPKINTUnique message identifier
account_idINTCustomer account associated with the message
sent_atTIMESTAMPTimestamp when the message was sent
message_typeVARCHAR(30)Message channel or type
Tablescustomer_accountsmessages
Interviewer

Your question is Rolling 7-Day Messages Per Account. 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.