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
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.

Output details

  • Return columns in this order: channel, metric_date, daily_conversions, previous_day_conversions, conversion_change, conversion_change_pct.
  • Sort by channel ascending, then metric_date ascending.
  • Round conversion_change_pct to two decimal places.

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

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.