Variant Click-Through Rate from Events

Quick Overview

Write a SQL query that counts impressions and actions for each A/B test variant in a raw event log and computes the click-through rate. It tests conditional aggregation, avoiding integer division, and handling a variant that has no impressions.

Variant Click-Through Rate from Events

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `events` table has one row per logged event, with its `variant` (`'A'` or `'B'`) and `event_type` (`'impression'` or `'action'`). Return one row per `variant` with `variant`, `impressions` (the count of `'impression'` rows), `actions` (the count of `'action'` rows), and `ctr` = `actions / impressions` rounded to 4 decimals. ```hint Two counts in one pass Both counts come from the same group of rows. Count each event type separately inside one aggregation, and check what your engine does when it divides one integer by another. ``` ### Input Table(s) `events` | Column | Type | Meaning | |---|---|---| | variant | VARCHAR(5) | Experiment variant, `'A'` or `'B'` | | event_type | VARCHAR(20) | `'impression'` or `'action'` | `events` has no primary key. Each row is one event, and identical rows are separate events. ### Output Contract Return these columns, in this order: | Column | Meaning | |---|---| | variant | The variant label | | impressions | Number of `'impression'` rows for the variant | | actions | Number of `'action'` rows for the variant | | ctr | `actions / impressions` as a decimal, rounded to 4 decimal places, or NULL when `impressions` is 0 | Return one row for each variant that has at least one row in `events`, ordered by `variant` ascending. ### Example Example 1. Input `events`: | variant | event_type | |---|---| | A | impression | | A | action | | B | impression | | B | impression | | B | impression | | A | action | | B | action | | B | impression | | A | action | | A | action | | A | impression | | B | impression | | B | impression | | A | action | | B | impression | | A | impression | | A | impression | | B | impression | | B | impression | | A | impression | | A | impression | | B | impression | | B | impression | Expected output: | variant | impressions | actions | ctr | |---|---|---|---| | A | 6 | 5 | 0.8333 | | B | 11 | 1 | 0.0909 | Variant A has 6 impressions and 5 actions, so its CTR is 5 / 6 = 0.8333. Variant B has 11 impressions and 1 action, so its CTR is 1 / 11 = 0.0909. Example 2. Input `events`: | variant | event_type | |---|---| | A | impression | | A | action | | B | action | Expected output: | variant | impressions | actions | ctr | |---|---|---|---| | A | 1 | 1 | 1.0000 | | B | 0 | 1 | NULL | Variant B has no impressions, so its CTR is undefined and is returned as NULL. ### Constraints and Clarifications - `variant` and `event_type` are never NULL, and `event_type` takes only the two listed values. - A variant with no impressions gets `ctr` NULL, and its `impressions` is 0. - The CTR is a ratio of event counts and is not capped at 1.

Overview: Write a SQL query that counts impressions and actions for each A/B test variant in a raw event log and computes the click-through rate. It tests conditional aggregation, avoiding integer division, and handling a variant that has no impressions.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The events table has one row per logged event, with its variant ('A' or 'B') and event_type ('impression' or 'action'). Return one row per variant with variant, impressions (the count of 'impression' rows), actions (the count of 'action' rows), and ctr = actions / impressions rounded to 4 decimals.

Input Table(s)

events

ColumnTypeMeaning
variantVARCHAR(5)Experiment variant, 'A' or 'B'
event_typeVARCHAR(20)'impression' or 'action'

events has no primary key. Each row is one event, and identical rows are separate events.

Output Contract

Return these columns, in this order:

ColumnMeaning
variantThe variant label
impressionsNumber of 'impression' rows for the variant
actionsNumber of 'action' rows for the variant
ctractions / impressions as a decimal, rounded to 4 decimal places, or NULL when impressions is 0

Return one row for each variant that has at least one row in events, ordered by variant ascending.

Example

Example 1. Input events:

variantevent_type
Aimpression
Aaction
Bimpression
Bimpression
Bimpression
Aaction
Baction
Bimpression
Aaction
Aaction
Aimpression
Bimpression
Bimpression
Aaction
Bimpression
Aimpression
Aimpression
Bimpression
Bimpression
Aimpression
Aimpression
Bimpression
Bimpression

Expected output:

variantimpressionsactionsctr
A650.8333
B1110.0909

Variant A has 6 impressions and 5 actions, so its CTR is 5 / 6 = 0.8333. Variant B has 11 impressions and 1 action, so its CTR is 1 / 11 = 0.0909.

Example 2. Input events:

variantevent_type
Aimpression
Aaction
Baction

Expected output:

variantimpressionsactionsctr
A111.0000
B01NULL

Variant B has no impressions, so its CTR is undefined and is returned as NULL.

Constraints and Clarifications

  • variant and event_type are never NULL, and event_type takes only the two listed values.
  • A variant with no impressions gets ctr NULL, and its impressions is 0.
  • The CTR is a ratio of event counts and is not capped at 1.