Compute Campaign Click-Through Rate Without Join Fan-Out Inflation
Last updated: November 29, 2026
Quick Overview
A query joins campaigns to daily impressions and to individual clicks and reports CTR, but totals look too high. Explain the bug and write a query that computes correct impressions, clicks and CTR per campaign.
0
0
0 solved
A query joins campaigns to daily impressions and to individual clicks and reports CTR, but totals look too high. Explain the bug and write a query that computes correct impressions, clicks and CTR per campaign.
Fan-out is one of the most common real bugs in Google Ads reporting queries, and technical screens use it to check whether you reason about join grain. The interviewer expects you to spot that joining two one-to-many tables multiplies rows, then fix it by aggregating each side to the campaign grain first.
What the Interviewer Expects
- Identify that two one-to-many joins multiply rows
- Show a concrete example of the inflated numbers
- Aggregate impressions and clicks separately to campaign grain before joining
- Keep campaigns with no impressions or clicks via LEFT JOIN
- Exclude invalid clicks and guard CTR division
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you detect fan-out automatically in a reporting pipeline?
- Why might CTR differ between your query and the Ads UI even after the fix?
- How would you compute CTR per day per campaign?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
campaigns(campaign_id, advertiser_id), impressions_daily(campaign_id, ds, impressions) (one row per campaign per day), and clicks(click_id, campaign_id, click_ts, is_invalid) (one row per click)...
Approach
Joining impressions (one row per day) and clicks (one row per click) on campaign alone pairs every day with every click. With 2 days and 2 clicks for a campaign, there are 4 joined rows, so impression...