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
Optimize Slow Join Queries
00:00
5 left

Optimize Slow Join Queries

MediumSQL · PostgreSQL

Problem

Explain how you would optimize a slow-running query that joins multiple high-volume tables, such as TikTok user views and ad impressions.

Use the provided tables to produce campaign-level daily metrics for active users during the specified date range. The result must avoid double-counting caused by joining multiple event rows for the same user and campaign.

Output

  1. One row per campaign and event date with activity.
  2. Columns: campaign_id, campaign_name, report_date, view_count, unique_viewers, impression_count, unique_impression_users, and overlap_users.
  3. Include campaigns with views or impressions, order by report_date, then campaign_id.

Schema

users
ColumnTypeDescription
user_idPKINTTikTok user identifier
account_statusVARCHAR(20)Current account status
countryVARCHAR(40)User country
campaigns
ColumnTypeDescription
campaign_idPKINTAdvertising campaign identifier
campaign_nameVARCHAR(100)Campaign display name
user_views
ColumnTypeDescription
view_idPKINTUnique view event identifier
user_idINTUser who generated the view
campaign_idINTAssociated campaign
event_dateDATEDate of the view event
ad_impressions
ColumnTypeDescription
impression_idPKINTUnique impression event identifier
user_idINTUser who received the impression
campaign_idINTAssociated campaign
event_dateDATEDate of the impression event
Tablesuserscampaignsuser_viewsad_impressions
Interviewer

Your question is Optimize Slow Join Queries. Start with the requirements and the four 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.