Revenue by Country at Purchase Time with a History Table
Last updated: November 29, 2026
Quick Overview
Users can change their country, recorded in a slowly changing dimension table with valid_from and valid_to. Write SQL that reports October Meta Verified subscription revenue by the country each user had at the moment of each payment.
Meta
November 29, 20260
0
0 solved
Users can change their country, recorded in a slowly changing dimension table with valid_from and valid_to. Write SQL that reports October Meta Verified subscription revenue by the country each user had at the moment of each payment.
Point-in-time joins against Type 2 dimension tables are a core data engineering pattern in Meta's warehouse, used for finance reporting where attributes change over time. The interviewer checks the range join, open-ended current rows, half-open intervals, and validation for gaps and overlaps in the history.
What the Interviewer Expects
- Joins on user and the timestamp falling inside the validity interval
- Handles current rows with NULL valid_to
- Uses half-open intervals to avoid double matches at boundaries
- Keeps payments with no matching history as UNKNOWN
- Adds checks for overlapping or missing history
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 overlapping intervals in the history table?
- How would the result differ if you joined on the user's current country?
- How would you build the history table from a daily snapshot of user profiles?
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
Tables:
payments(payment_id, user_id, paid_at, amount_usd)user_country_history(user_id, country, valid_from, valid_to)wherevalid_tois NULL for the current row, and intervals are $[\text{...
Approach
Join each payment to the history row for that user whose interval contains paid_at. Using the current country instead would move a user's historical revenue every time they move, which breaks financ...