Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
Channel Performance by Time in SQL
00:00
5 left

Channel Performance by Time in SQL

MediumSQL · PostgreSQL

Problem

HelloFresh marketing teams need a monthly view of campaign performance across acquisition channels. Write a PostgreSQL query that combines campaign metadata with daily campaign metrics.

Requirements

  1. Aggregate impressions, clicks, conversions, spend, and revenue by calendar month and channel for Q1 2025.
  2. Replace a missing channel with Unknown.
  3. Calculate click-through rate, conversion rate, and return on ad spend. Avoid division-by-zero errors and preserve NULL when a ratio cannot be calculated.
  4. Order the results chronologically, then alphabetically by channel.

Output details

  • Return columns in this order: campaign_month, channel, total_impressions, total_clicks, total_conversions, total_spend, total_revenue, ctr_pct, conversion_rate_pct, roas.
  • Format campaign_month as YYYY-MM.
  • Filter Q1 2025 from 2025-01-01 inclusive through 2025-04-01 exclusive.
  • Round ctr_pct, conversion_rate_pct, and roas to two decimal places, expressing the first two as percentages.
  • Sort by month ascending, then channel ascending.
  • Preserve NULL metric totals when all contributing values are NULL rather than converting them to zero.

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTUnique campaign identifier
campaign_nameVARCHAR(100)HelloFresh campaign name
channelVARCHAR(50)Marketing acquisition channel
campaign_metrics
ColumnTypeDescription
metric_idPKINTUnique daily metric identifier
campaign_idINTReferences campaigns.campaign_id
metric_dateDATEDate represented by the metrics
impressionsINTNumber of delivered impressions
clicksINTNumber of campaign clicks
conversionsINTNumber of attributed conversions
spendDECIMAL(10,2)Advertising spend
revenueDECIMAL(10,2)Attributed revenue
Tablescampaignscampaign_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.