← Meta Interview Insights

Meta·Data Scientist·Technical Phone Screen·Senior

Senior
May 2026

Summary

SQL round for a DS role at Meta, one question but it had enough moving parts to keep me busy for a while. The ad metrics angle felt pretty domain-specific for a marketplace context.

Questions Asked (1)

Q1

Write a SQL query that produces daily ad performance broken down by country for the last 7 complete UTC days. The output should include impressions, clicks, revenue, CTR, and RPM per (date, country) pair, joining impression and click data correctly and handling impressions with no clicks.

Product Analytics & MetricsData Modeling
Author's notes

There's a lot going on here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining the date range for the last 7 complete UTC days using a subquery or CTE that calculates the current date in UTC and subtracts 7 days. Then aggregate impressions and clicks separately by date and country, and perform a LEFT JOIN from impressions to clicks to handle cases with no clicks. Finally, compute CTR and RPM using the aggregated metrics, ensuring proper handling of division by zero.

Pro tip: Always clarify the definition of 'last 7 complete UTC days'—it typically means from 00:00:00 UTC 7 days ago up to but not including 00:00:00 UTC today. Also, consider using COALESCE or NULLIF to avoid division by zero errors when calculating CTR and RPM.

1. Define the date range

Calculate the start and end dates for the last 7 complete UTC days. Use UTC_DATE() or equivalent to get today's date in UTC, then subtract 7 days for the start and exclude today.

2. Aggregate impressions and clicks separately

Create two subqueries or CTEs: one that sums impressions by date and country, and another that sums clicks by date and country. This ensures correct aggregation before joining.

3. Join the aggregated data

Perform a LEFT JOIN from the impressions aggregation to the clicks aggregation on date and country, so that impressions with no clicks are retained with zero clicks.

4. Calculate derived metrics

Compute CTR as clicks divided by impressions (using NULLIF to avoid division by zero) and RPM as revenue divided by impressions times 1000. Ensure revenue is included in the impressions aggregation or joined appropriately.

5. Format and order the output

Select the date, country, impressions, clicks, revenue, CTR, and RPM. Order by date and country for readability.

Key Points to Mention

  • Use of UTC date functions to define the correct time window
  • Separate aggregation of impressions and clicks before joining to avoid fan-out
  • LEFT JOIN to handle impressions with no clicks
  • Handling division by zero with NULLIF or CASE statements
  • Calculation of CTR as clicks/impressions and RPM as (revenue/impressions)*1000
  • Consideration of data types and rounding for metrics

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.