Top-2 Campaigns by CTR per Advertiser

SQL practice question · Hard · Window Functions · Google

`ad_stats` holds hourly impression/click counts per campaign, but some hours were loaded more than once: for the same (campaign_id, stat_hour) keep only the row with the HIGHEST `stat_id` (latest load). After de-duplication, total impressions and clicks per campaign; only campaigns with at least 100 impressions qualify. CTR = 100.0 * clicks / impressions. Rank qualifying campaigns within each advertiser by CTR (highest first) with DENSE_RANK and keep ranks 1 and 2. Return `advertiser`, `campaign_name`, `impressions`, `clicks`, `ctr_pct` (ROUND 2), `rnk`. Order by `advertiser`, `rnk`, `campaign_name`.

Table: campaigns

Schema of campaigns
ColumnType
campaign_idINTEGER
advertiserTEXT
campaign_nameTEXT
Sample rows of campaigns
campaign_idadvertisercampaign_name
1AcmeAcme Shoes
2AcmeAcme Socks
3AcmeAcme Hats
4AcmeAcme Belts
5BetaBeta Phones
6BetaBeta Cases
7BetaBeta Cables

Table: ad_stats

Schema of ad_stats
ColumnType
stat_idINTEGER
campaign_idINTEGER
stat_hourTIMESTAMP
impressionsINTEGER
clicksINTEGER
Sample rows of ad_stats
stat_idcampaign_idstat_hourimpressionsclicks
112026-10-01 09:00:001005
212026-10-01 10:00:001003
322026-10-01 09:00:00501
432026-10-01 09:00:001206
532026-10-01 09:00:001209
642026-10-01 09:00:0030012
752026-10-01 09:00:004008
862026-10-01 09:00:001001
Expected output on the sample data
advertisercampaign_nameimpressionsclicksctr_pctrnk
AcmeAcme Hats12097.51
AcmeAcme Belts3001242
AcmeAcme Shoes200842
BetaBeta Phones400821
BetaBeta Cases100112

Solve it in the live editor on this page — run on sample data, then submit against hidden tests.

‹ Azure Cost Roll-up Through the Resource Tree · Clicks per Normalized Domain ›

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Question Asked in Interview Blog
SQL Interview Practice

Company-wise SQL questions

{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.

🏆 Weekly SQL Contest
{{ totalCount }}
Questions
{{ companyCount }}
Companies
{{ prog.total.solved }}
Solved
Bookmark Question Companies Difficulty Category Status
{{ r.n }} - {{ r.title }} {{ c }} {{ r.difficulty }} {{ r.category }} {{ r.statusIcon }}
No questions match these filters.
Showing {{ rangeLabel }} of {{ filteredCount }}

Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.

← All questions

{{ cur.n }} - {{ cur.title }}

🏆 Contest {{ contestWeek }}
{{ cur.catLabel }} {{ cur.diffLabel }} {{ c }}
Embed this questionPaste on a blog, course page or LMS. Free to use — educator guide
{{ embedCode }}

{{ cur.statement }}

Table: {{ tb.name }}
ColumnType {{ c.name }}{{ c.type }}
{{ h }} {{ v.text }}
Expected output on the sample data:
{{ h }} {{ v.text }}
  • {{ h }}

No submissions yet. Press Evaluate to submit your query.

{{ s.verdict }} {{ s.meta }}
The solution unlocks after you solve this question or after 3 evaluations that didn't pass ({{ failCount }}/3 so far). Trying first is the fastest way to learn.
{{ solutionSql }}
  1. {{ e }}
⏱ {{ timerLabel }}
{{ fnHelpTitle }}
{{ fnHelpText }}
Writing {{ dialectName }} · Ctrl/Cmd + Enter runs the query · Tab indents · your code is saved automatically
Output {{ out.status }}
  • {{ w }}
{{ out.verdict }}
{{ out.detail }}
{{ out.error }}
Expected (sample data)
{{ h }}{{ v.text }}
Your output (sample data)
{{ h }}{{ v.text }}
{{ h }} {{ v.text }}
Run your query to see the result here.
🔒

To Track Your Progress, Log In / Sign Up

You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.

Sign Up Free Log In