Compute daily treatment and control conversion rates and their difference in SQL

Quick Overview

Write a PostgreSQL query that turns raw A/B test exposure rows into daily treatment and control conversion rates and the gap between them. It tests conditional aggregation, safe division with NULLIF, numeric rounding, and handling days where one variant has no traffic.

Compute daily treatment and control conversion rates and their difference in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `events` table has one row per user exposure in an A/B test, with the exposure timestamp `ts`, the `variant` the user saw (`'control'` or `'treatment'`) and whether the user `converted` (1 or 0). Return one row per `day` = `DATE(ts)` with `treatment_rate`, `control_rate` (each one is conversions divided by users within that day and variant) and `rate_diff` = `treatment_rate - control_rate`. Round the three rate columns to 4 decimals. ```hint Getting both variants into one row You can get each variant's counts side by side from a single GROUP BY, without a join. Check what type your division returns before you round it. ``` ### Input Table(s) Table `events`: | Column | Type | Meaning | |---|---|---| | ts | TIMESTAMP | When the user was exposed | | variant | VARCHAR(20) | `'control'` or `'treatment'` | | converted | SMALLINT | 1 if the user converted, 0 otherwise | There is no primary key. Each row is one user exposure, and several rows can share the same `ts`, `variant` and `converted` values. ### Output Contract - Columns, in this order: `day` (DATE), `treatment_rate`, `control_rate`, `rate_diff`. - One row for each day that has at least one row in `events`. - Each rate is the number of rows with `converted = 1` divided by the number of rows, within that day and variant. - `rate_diff` is computed from the unrounded rates and then rounded. - All three rate columns are rounded to 4 decimals. - Rows are sorted by `day` ascending. ### Example Input: ```text events +---------------------+-----------+-----------+ | ts | variant | converted | +---------------------+-----------+-----------+ | 2026-03-02 16:00:00 | control | 0 | | 2026-03-02 09:00:00 | control | 1 | | 2026-03-01 08:00:00 | control | 1 | | 2026-03-03 10:00:00 | control | 0 | | 2026-03-01 15:00:00 | control | 1 | | 2026-03-03 17:00:00 | treatment | 0 | | 2026-03-03 10:00:00 | treatment | 1 | | 2026-03-01 15:00:00 | treatment | 1 | | 2026-03-01 08:00:00 | treatment | 1 | | 2026-03-01 08:00:00 | control | 0 | | 2026-03-01 15:00:00 | treatment | 1 | | 2026-03-02 16:00:00 | control | 1 | | 2026-03-02 09:00:00 | control | 0 | | 2026-03-03 17:00:00 | treatment | 1 | | 2026-03-02 16:00:00 | treatment | 1 | | 2026-03-03 17:00:00 | treatment | 1 | | 2026-03-02 16:00:00 | control | 0 | | 2026-03-01 15:00:00 | treatment | 1 | | 2026-03-02 09:00:00 | treatment | 0 | | 2026-03-03 10:00:00 | treatment | 0 | | 2026-03-03 17:00:00 | control | 1 | +---------------------+-----------+-----------+ ``` Expected output: ```text +------------+----------------+--------------+-----------+ | day | treatment_rate | control_rate | rate_diff | +------------+----------------+--------------+-----------+ | 2026-03-01 | 1.0000 | 0.6667 | 0.3333 | | 2026-03-02 | 0.5000 | 0.4000 | 0.1000 | | 2026-03-03 | 0.6000 | 0.5000 | 0.1000 | +------------+----------------+--------------+-----------+ ``` On 2026-03-01, treatment has 4 rows and all 4 converted (rate 1), while control has 3 rows with 2 conversions (rate 2/3). On 2026-03-02, treatment converts 1 of 2 and control 2 of 5. On 2026-03-03, treatment converts 3 of 5 and control 1 of 2. ### Constraints and Clarifications - Every row counts as one user. There is no user id column, so rows that look identical are separate exposures and all of them count. - `variant` is always `'control'` or `'treatment'`, and `converted` is never NULL. - If a day has rows for only one variant, the other variant's rate is NULL and `rate_diff` is NULL. That day still appears in the output. - `day` is the calendar date of `ts` as stored, with no time zone conversion. - Rate values are compared as numbers, so `1` and `1.0000` are the same result.

Overview: Write a PostgreSQL query that turns raw A/B test exposure rows into daily treatment and control conversion rates and the gap between them. It tests conditional aggregation, safe division with NULLIF, numeric rounding, and handling days where one variant has no traffic.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

The events table has one row per user exposure in an A/B test, with the exposure timestamp ts, the variant the user saw ('control' or 'treatment') and whether the user converted (1 or 0). Return one row per day = DATE(ts) with treatment_rate, control_rate (each one is conversions divided by users within that day and variant) and rate_diff = treatment_rate - control_rate. Round the three rate columns to 4 decimals.

Input Table(s)

Table events:

ColumnTypeMeaning
tsTIMESTAMPWhen the user was exposed
variantVARCHAR(20)'control' or 'treatment'
convertedSMALLINT1 if the user converted, 0 otherwise

There is no primary key. Each row is one user exposure, and several rows can share the same ts, variant and converted values.

Output Contract

  • Columns, in this order: day (DATE), treatment_rate , control_rate , rate_diff .
  • One row for each day that has at least one row in events .
  • Each rate is the number of rows with converted = 1 divided by the number of rows, within that day and variant.
  • rate_diff is computed from the unrounded rates and then rounded.
  • All three rate columns are rounded to 4 decimals.
  • Rows are sorted by day ascending.

Example

Input:

events
+---------------------+-----------+-----------+
| ts                  | variant   | converted |
+---------------------+-----------+-----------+
| 2026-03-02 16:00:00 | control   | 0         |
| 2026-03-02 09:00:00 | control   | 1         |
| 2026-03-01 08:00:00 | control   | 1         |
| 2026-03-03 10:00:00 | control   | 0         |
| 2026-03-01 15:00:00 | control   | 1         |
| 2026-03-03 17:00:00 | treatment | 0         |
| 2026-03-03 10:00:00 | treatment | 1         |
| 2026-03-01 15:00:00 | treatment | 1         |
| 2026-03-01 08:00:00 | treatment | 1         |
| 2026-03-01 08:00:00 | control   | 0         |
| 2026-03-01 15:00:00 | treatment | 1         |
| 2026-03-02 16:00:00 | control   | 1         |
| 2026-03-02 09:00:00 | control   | 0         |
| 2026-03-03 17:00:00 | treatment | 1         |
| 2026-03-02 16:00:00 | treatment | 1         |
| 2026-03-03 17:00:00 | treatment | 1         |
| 2026-03-02 16:00:00 | control   | 0         |
| 2026-03-01 15:00:00 | treatment | 1         |
| 2026-03-02 09:00:00 | treatment | 0         |
| 2026-03-03 10:00:00 | treatment | 0         |
| 2026-03-03 17:00:00 | control   | 1         |
+---------------------+-----------+-----------+

Expected output:

+------------+----------------+--------------+-----------+
| day        | treatment_rate | control_rate | rate_diff |
+------------+----------------+--------------+-----------+
| 2026-03-01 | 1.0000         | 0.6667       | 0.3333    |
| 2026-03-02 | 0.5000         | 0.4000       | 0.1000    |
| 2026-03-03 | 0.6000         | 0.5000       | 0.1000    |
+------------+----------------+--------------+-----------+

On 2026-03-01, treatment has 4 rows and all 4 converted (rate 1), while control has 3 rows with 2 conversions (rate 2/3). On 2026-03-02, treatment converts 1 of 2 and control 2 of 5. On 2026-03-03, treatment converts 3 of 5 and control 1 of 2.

Constraints and Clarifications

  • Every row counts as one user. There is no user id column, so rows that look identical are separate exposures and all of them count.
  • variant is always 'control' or 'treatment' , and converted is never NULL.
  • If a day has rows for only one variant, the other variant's rate is NULL and rate_diff is NULL. That day still appears in the output.
  • day is the calendar date of ts as stored, with no time zone conversion.
  • Rate values are compared as numbers, so 1 and 1.0000 are the same result.