Pooled conversion proportion across control and treatment in SQL

Quick Overview

Write a SQL query that returns the total conversions, the total users and the pooled conversion proportion across the control and treatment arms of an A/B test table. It tests aggregate functions, avoiding integer division, and rounding a ratio to a fixed number of decimals.

Pooled conversion proportion across control and treatment in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `trials` table has one row per user in an A/B test. `group_name` is the arm the user was assigned to (`'control'` or `'treatment'`) and `converted` is 1 if the user converted and 0 otherwise. Compute the pooled conversion proportion across both arms. Return a single row with `conversions_total` (all conversions), `users_total` (all users), and `pooled_p` = `conversions_total` / `users_total` rounded to 4 decimals. ```hint Watch the division Both totals are integers. Check what your engine returns when it divides one integer by another, and do that before you think about rounding. ``` ### Input Table(s) Table `trials`: | Column | Type | Meaning | |---|---|---| | group_name | VARCHAR(20) | Arm the user was assigned to: `'control'` or `'treatment'` | | converted | SMALLINT | 1 if the user converted, 0 otherwise | There is no key column. Each row is one user, and no user appears twice. ### Output Contract Exactly one row with these columns, in this order: | Column | Type | Meaning | |---|---|---| | conversions_total | integer | Number of users with `converted = 1`, across both arms | | users_total | integer | Number of users (rows) in `trials` | | pooled_p | numeric | `conversions_total / users_total`, rounded to 4 decimal places | The result has a single row, so no ordering is needed. ### Example Input `trials`: | group_name | converted | |---|---| | treatment | 1 | | control | 1 | | treatment | 1 | | treatment | 0 | | control | 1 | | treatment | 0 | | control | 1 | | treatment | 0 | | control | 0 | | control | 0 | | control | 0 | | treatment | 0 | | treatment | 0 | | treatment | 1 | | control | 0 | | control | 0 | | treatment | 1 | | control | 0 | | control | 1 | Expected output: | conversions_total | users_total | pooled_p | |---|---|---| | 8 | 19 | 0.4211 | Treatment has 4 conversions out of 9 users and control has 4 out of 10, so the totals are 8 conversions and 19 users. 8 / 19 = 0.42105..., which rounds to 0.4211. ### Constraints and Clarifications - `converted` is always 0 or 1 and is never NULL. - Assume `trials` has at least one row. - The arm does not change the result. Every user counts once in the totals, whichever group they are in. - Round to 4 decimal places with half away from zero, which is what `ROUND` does on a `NUMERIC` value.

Overview: Write a SQL query that returns the total conversions, the total users and the pooled conversion proportion across the control and treatment arms of an A/B test table. It tests aggregate functions, avoiding integer division, and rounding a ratio to a fixed number of decimals.

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

The trials table has one row per user in an A/B test. group_name is the arm the user was assigned to ('control' or 'treatment') and converted is 1 if the user converted and 0 otherwise. Compute the pooled conversion proportion across both arms. Return a single row with conversions_total (all conversions), users_total (all users), and pooled_p = conversions_total / users_total rounded to 4 decimals.

Input Table(s)

Table trials:

ColumnTypeMeaning
group_nameVARCHAR(20)Arm the user was assigned to: 'control' or 'treatment'
convertedSMALLINT1 if the user converted, 0 otherwise

There is no key column. Each row is one user, and no user appears twice.

Output Contract

Exactly one row with these columns, in this order:

ColumnTypeMeaning
conversions_totalintegerNumber of users with converted = 1, across both arms
users_totalintegerNumber of users (rows) in trials
pooled_pnumericconversions_total / users_total, rounded to 4 decimal places

The result has a single row, so no ordering is needed.

Example

Input trials:

group_nameconverted
treatment1
control1
treatment1
treatment0
control1
treatment0
control1
treatment0
control0
control0
control0
treatment0
treatment0
treatment1
control0
control0
treatment1
control0
control1

Expected output:

conversions_totalusers_totalpooled_p
8190.4211

Treatment has 4 conversions out of 9 users and control has 4 out of 10, so the totals are 8 conversions and 19 users. 8 / 19 = 0.42105..., which rounds to 0.4211.

Constraints and Clarifications

  • converted is always 0 or 1 and is never NULL.
  • Assume trials has at least one row.
  • The arm does not change the result. Every user counts once in the totals, whichever group they are in.
  • Round to 4 decimal places with half away from zero, which is what ROUND does on a NUMERIC value.