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
CTR Query by Segment
00:00
5 left

CTR Query by Segment

MediumSQL · PostgreSQL

Problem

Write a SQL query to calculate the click-through rate (CTR) of ads segmented by industry and company size over the last 30 days for LinkedIn marketing campaigns.

Use the provided LinkedIn Ads, LinkedIn Companies, and LinkedIn Ad Events tables. Include only segments with at least one impression during the period. CTR is clicks divided by impressions, expressed as a percentage.

Output

  1. One row per industry and company size segment.
  2. Columns: industry, company_size, impressions, clicks, and ctr_percent.
  3. Include only events from the last 30 days through today and segments with impressions greater than zero.
  4. Order by industry ascending with null industries last, then company_size ascending.

Schema

linkedin_ads
ColumnTypeDescription
ad_idPKINTUnique LinkedIn ad identifier
campaign_idINTLinkedIn marketing campaign identifier
company_idINTAdvertiser company identifier
ad_nameVARCHAR(100)Human-readable ad name
linkedin_companies
ColumnTypeDescription
company_idPKINTUnique LinkedIn company identifier
industryVARCHAR(100)Company industry
company_sizeVARCHAR(50)Company size segment
linkedin_ad_events
ColumnTypeDescription
event_idPKINTUnique ad event record identifier
ad_idINTReferenced LinkedIn ad
event_dateDATEDate on which the event occurred
event_typeVARCHAR(20)Event type, such as impression or click
event_countINTNumber of events represented by the row
Tableslinkedin_adslinkedin_companieslinkedin_ad_events
Interviewer

Your question is CTR Query by Segment. Start with the requirements and the three 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.