PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches
|Home/Data Manipulation (SQL/Python)/Meta

Compute ad revenue metrics by geography in SQL

Last updated: Mar 29, 2026

Quick Overview

In the Data Manipulation (SQL/Python) domain, this intermediate-level question evaluates SQL aggregation, join logic between impressions and clicks, date-based UTC attribution, user-geo joins, and null/divide-by-zero-safe computation of derived metrics (CTR, RPM) at the country-day granularity.

  • medium
  • Meta
  • Data Manipulation (SQL/Python)
  • Data Scientist

Compute ad revenue metrics by geography in SQL

Company: Meta

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Technical Screen

You work on a marketplace app that shows shop ads. You are given the following tables. **Assumptions** - All timestamps are stored in UTC. - “Revenue” is the money earned by the platform from an ad click (CPC) and is recorded at click time. - CTR = clicks / impressions. - RPM = 1000 * revenue / impressions. **Tables** `ad_impressions` - `impression_id` BIGINT - `user_id` BIGINT - `shop_id` BIGINT - `impression_ts` TIMESTAMP (UTC) `ad_clicks` - `click_id` BIGINT - `impression_id` BIGINT -- foreign key to `ad_impressions.impression_id` - `click_ts` TIMESTAMP (UTC) - `revenue_usd` NUMERIC(10,2) -- platform revenue from this click `user_geo_daily` - `user_id` BIGINT - `geo_date` DATE -- in UTC - `country_code` STRING **Task** Write a SQL query to produce **daily ad performance by country** for the last 7 complete UTC days (excluding today). Output one row per (`event_date`, `country_code`) with: - `event_date` (DATE, UTC) - `country_code` - `impressions` - `clicks` - `revenue_usd` - `ctr` (as a decimal) - `rpm` (as a decimal) Details: - Attribute an impression to `event_date = DATE(impression_ts)`. - Attribute an impression to a country using `user_geo_daily` where `geo_date = event_date`. - Join clicks to impressions via `impression_id`. - If an impression has no clicks, it should still count toward impressions with 0 clicks and 0 revenue. - Avoid divide-by-zero errors for CTR/RPM.

Quick Answer: In the Data Manipulation (SQL/Python) domain, this intermediate-level question evaluates SQL aggregation, join logic between impressions and clicks, date-based UTC attribution, user-geo joins, and null/divide-by-zero-safe computation of derived metrics (CTR, RPM) at the country-day granularity.

Related Interview Questions

  • Compute ad impression conversion rates - Meta (medium)
  • Count unconnected posts and reactions - Meta (medium)
  • Count heavy callers in 7 days - Meta (medium)
  • Write SQL for call metrics - Meta (medium)
  • Write SQL for multi-account metrics - Meta (medium)
Meta logo
Meta
Oct 14, 2025, 12:00 AM
Data Scientist
Technical Screen
Data Manipulation (SQL/Python)
5
0

You work on a marketplace app that shows shop ads. You are given the following tables.

Assumptions

  • All timestamps are stored in UTC.
  • “Revenue” is the money earned by the platform from an ad click (CPC) and is recorded at click time.
  • CTR = clicks / impressions.
  • RPM = 1000 * revenue / impressions.

Tables

ad_impressions

  • impression_id BIGINT
  • user_id BIGINT
  • shop_id BIGINT
  • impression_ts TIMESTAMP (UTC)

ad_clicks

  • click_id BIGINT
  • impression_id BIGINT -- foreign key to ad_impressions.impression_id
  • click_ts TIMESTAMP (UTC)
  • revenue_usd NUMERIC(10,2) -- platform revenue from this click

user_geo_daily

  • user_id BIGINT
  • geo_date DATE -- in UTC
  • country_code STRING

Task Write a SQL query to produce daily ad performance by country for the last 7 complete UTC days (excluding today). Output one row per (event_date, country_code) with:

  • event_date (DATE, UTC)
  • country_code
  • impressions
  • clicks
  • revenue_usd
  • ctr (as a decimal)
  • rpm (as a decimal)

Details:

  • Attribute an impression to event_date = DATE(impression_ts) .
  • Attribute an impression to a country using user_geo_daily where geo_date = event_date .
  • Join clicks to impressions via impression_id .
  • If an impression has no clicks, it should still count toward impressions with 0 clicks and 0 revenue.
  • Avoid divide-by-zero errors for CTR/RPM.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Data Manipulation (SQL/Python)•More Meta•More Data Scientist•Meta Data Scientist•Meta Data Manipulation (SQL/Python)•Data Scientist Data Manipulation (SQL/Python)
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.