The join logic is where I spent most of my mental energy.
Start by clarifying the table schemas and grain (e.g., each impression/click/conversion event has ad_id and timestamp). Then design a query that aggregates each event type to ad_id and date, joins them on ad_id and date, and computes CTR and conversion rate with proper handling of zero denominators. Use a date filter for the last 7 days and order by date and ad_id.
Pro tip: Always guard against division by zero using NULLIF or CASE, and consider whether the last 7 days should be based on the current date or the max date in the data—clarify this with the interviewer to show attention to detail.
Ask about the table structures: what columns exist, how events are recorded (e.g., one row per impression/click/conversion), and whether timestamps are in UTC. Confirm the definition of 'last 7 days' (e.g., relative to current date or max date in data).
Write subqueries or CTEs to count impressions, clicks, and conversions per ad_id and calendar date. Ensure you filter each to the last 7 days to reduce data volume.
Full outer join the aggregated tables on ad_id and date to ensure ads with only some event types are included. Use COALESCE to replace NULLs with 0 for counts.
Calculate CTR as clicks / impressions and conversion rate as conversions / clicks (or impressions, depending on definition). Use NULLIF or CASE to avoid division by zero, returning NULL or 0 as appropriate.
Order the final result by date and ad_id. Select only the required columns: date, ad_id, impressions, clicks, conversions, CTR, and conversion rate.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.