Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL With LAG/LEAD

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

Your question is SQL With LAG/LEAD. 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

Blend360 analysts monitor campaign performance across marketing channels. Write a PostgreSQL query that summarizes conversions by channel and day, then compares each result with the previous recorded day for that channel.

Requirements

  1. Join campaign metadata to daily campaign metrics and include only active campaigns during January 2025.
  2. Aggregate conversions by channel and metric_date, treating rows where all conversion values are NULL as zero.
  3. Use LAG to return the previous recorded day's conversions for each channel.
  4. Calculate both the absolute conversion change and percentage change. Return NULL for the first available day or when the previous value is zero.
  5. Sort the results by channel and metric date.

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(100)Blend360 campaign name
channelVARCHAR(50)Marketing channel used by the campaign
is_activeBOOLEANWhether the campaign is currently active
daily_campaign_metrics
ColumnTypeDescription
metric_idPKINTUnique metric record identifier
campaign_idINTReferences campaigns.campaign_id
metric_dateDATEDate on which the metric was recorded
conversionsINTNumber of conversions recorded for the campaign
Tablescampaignsdaily_campaign_metrics
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results