Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Window Functions

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

Your question is SQL Window Functions. 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

Adastra Group analysts monitor campaign performance across its data and analytics engagements. Write a PostgreSQL query to calculate a three-day moving average of daily campaign spend for active campaigns.

Requirements

  1. Include only campaigns with status = 'active' and metrics recorded from 2024-01-01 through 2024-01-05.
  2. Join campaign metadata to daily metrics and return one row per campaign and metric date.
  3. Calculate the moving average using the current row and up to the two preceding rows within each campaign, ordered by metric_date.
  4. Round the moving average to two decimal places and order the final output by campaign ID and date.

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(100)Campaign display name
channelVARCHAR(50)Campaign marketing channel
statusVARCHAR(20)Campaign lifecycle status
campaign_daily_metrics
ColumnTypeDescription
metric_idPKINTUnique daily metric identifier
campaign_idINTReferences campaigns.campaign_id
metric_dateDATEDate of the daily measurement
daily_spendDECIMAL(10,2)Campaign spend recorded for the day
Tablescampaignscampaign_daily_metrics
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results