Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top Advertisers Per Month

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Top Advertisers Per Month. Start with the requirements and the two tables on the right.

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.

Problem

Sovrn wants to identify the advertisers contributing the most spend to its advertising marketplace each month. Write a PostgreSQL query that aggregates advertiser spend by calendar month and ranks advertisers within each month.

Requirements

  1. Restrict the analysis to January and February 2024.
  2. Calculate each advertiser's total spend per month, treating NULL spend amounts as zero.
  3. Use a ranking window function partitioned by month and return advertisers with a rank of 3 or better. Include ties at third place.
  4. Return the month as YYYY-MM, advertiser name, total spend, and rank, ordered chronologically by month and then by rank.

Schema

advertisers
ColumnTypeDescription
advertiser_idPKINTEGERUnique advertiser identifier
advertiser_nameVARCHAR(100)Advertiser display name
advertiser_spend
ColumnTypeDescription
spend_idPKINTEGERUnique spend record identifier
advertiser_idINTEGERAdvertiser associated with the spend record
spend_dateDATEDate on which spend was recorded
spend_amountNUMERIC(12,2)Amount spent by the advertiser
Tablesadvertisersadvertiser_spend
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results