SQL
Lab
Meta Meta Interview Question 04

Ad
Click-Through
Rate

Calculate CTR per ad.

Table Schema

Inspect
Table

interactive
ColumnType
ad_idinteger
impressionsinteger
clicksinteger

Sample Data

Input
Output

Sample Input: ads
ad_idimpressionsclicks
11000100
22000150
31500300
Expected Output
ad_idctr
10.1000
20.0750
30.2000

SQL Editor

Run
Query

postgresql
Waiting for query

ad_idctr
10.1000
20.0750
30.2000

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT ad_id, 1.0 * clicks / impressions AS ctr
FROM ads;

Explanation

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving