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
SQL With LAG/LEAD
00:00
5 left

SQL With LAG/LEAD

MediumSQL · PostgreSQL

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
Interviewer

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