The Ads Performance interview question is a SQL-based challenge focusing on digital marketing metrics. You are given an Ads table containing ad_id, user_id, and action (which can be 'Clicked', 'Viewed', or 'Ignored'). You need to calculate the Click-Through Rate (CTR) for each ad.
The CTR formula is: .
Meta (Facebook) asks this because it mirrors their actual business logic. It evaluates a candidate's ability to handle Conditional Aggregation (counting only specific rows), avoid division-by-zero errors, and format percentages correctly.
This follows the Database interview pattern of using CASE statements combined with SUM() or COUNT(). To handle the math properly:
IFNULL or COALESCE to handle cases where an ad has no clicks or views.ad_id and sort the results as specified (usually by CTR descending, then ad_id ascending).If Ad 1 has:
2/5 is 0. You must multiply by 100.0 or cast to a float to get a decimal result.Practice the CASE WHEN action = 'Clicked' THEN 1 ELSE 0 END pattern. It is the most robust way to perform counts based on specific string values in SQL.