Control vs Treatment Conversion Rate by Group in SQL

Quick Overview

Write a PostgreSQL query that reports users, conversions and the conversion rate for the control and treatment groups of an A/B test, one row per group. It tests grouped aggregation, avoiding integer division, rounding, and returning rows in a deterministic order.

Control vs Treatment Conversion Rate by Group in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `assignments` table holds one row per user in an A/B test. Each user is in either the `control` or the `treatment` group, and `converted` is 1 if the user converted and 0 otherwise. Return one row per group with `group_name`, `users` (the number of rows in the group), `conversions` (the sum of `converted`) and `conversion_rate` (`conversions / users`, rounded to 4 decimals). ```hint Check the division Find out what PostgreSQL returns when it divides one integer by another, and make sure the rate cannot come out as 0. ``` ### Input Table(s) Table `assignments`: | Column | Type | Meaning | |---|---|---| | `group_name` | VARCHAR(20) | Experiment arm, either `'control'` or `'treatment'` | | `converted` | SMALLINT | 1 if the user converted, 0 if not | The table has no key column. Each row is one user. ### Output Contract One row per group, with these columns in this order: | Column | Type | Rule | |---|---|---| | `group_name` | VARCHAR | The group | | `users` | BIGINT | Number of rows in the group | | `conversions` | BIGINT | Sum of `converted` in the group | | `conversion_rate` | NUMERIC | `conversions / users`, rounded to 4 decimal places | Order the rows by `group_name` ascending. Each group appears once, so that order is unique. ### Example Input `assignments`: | group_name | converted | |---|---| | treatment | 1 | | treatment | 1 | | control | 0 | | control | 1 | | control | 1 | | control | 1 | | treatment | 1 | | treatment | 0 | | treatment | 0 | | treatment | 1 | | control | 0 | | control | 0 | | control | 0 | | treatment | 1 | | control | 1 | | control | 1 | | treatment | 1 | | treatment | 0 | | treatment | 0 | | treatment | 1 | | control | 1 | | control | 0 | Expected output: | group_name | users | conversions | conversion_rate | |---|---|---|---| | control | 11 | 6 | 0.5455 | | treatment | 11 | 7 | 0.6364 | Control has 11 rows and 6 of them converted, and 6/11 = 0.54545... rounds to 0.5455. Treatment has 11 rows with 7 conversions, and 7/11 = 0.63636... rounds to 0.6364. ### Constraints and Clarifications - `group_name` only takes the values `'control'` and `'treatment'`, and both appear at least once. - `converted` is always 0 or 1 and never NULL. - There is no user id, so every row counts as one user. Do not deduplicate identical rows. - Rounding follows PostgreSQL's `ROUND` on NUMERIC, which rounds halves away from zero.

Overview: Write a PostgreSQL query that reports users, conversions and the conversion rate for the control and treatment groups of an A/B test, one row per group. It tests grouped aggregation, avoiding integer division, rounding, and returning rows in a deterministic order.

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

The assignments table holds one row per user in an A/B test. Each user is in either the control or the treatment group, and converted is 1 if the user converted and 0 otherwise. Return one row per group with group_name, users (the number of rows in the group), conversions (the sum of converted) and conversion_rate (conversions / users, rounded to 4 decimals).

Input Table(s)

Table assignments:

ColumnTypeMeaning
group_nameVARCHAR(20)Experiment arm, either 'control' or 'treatment'
convertedSMALLINT1 if the user converted, 0 if not

The table has no key column. Each row is one user.

Output Contract

One row per group, with these columns in this order:

ColumnTypeRule
group_nameVARCHARThe group
usersBIGINTNumber of rows in the group
conversionsBIGINTSum of converted in the group
conversion_rateNUMERICconversions / users, rounded to 4 decimal places

Order the rows by group_name ascending. Each group appears once, so that order is unique.

Example

Input assignments:

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

Expected output:

group_nameusersconversionsconversion_rate
control1160.5455
treatment1170.6364

Control has 11 rows and 6 of them converted, and 6/11 = 0.54545... rounds to 0.5455. Treatment has 11 rows with 7 conversions, and 7/11 = 0.63636... rounds to 0.6364.

Constraints and Clarifications

  • group_name only takes the values 'control' and 'treatment' , and both appear at least once.
  • converted is always 0 or 1 and never NULL.
  • There is no user id, so every row counts as one user. Do not deduplicate identical rows.
  • Rounding follows PostgreSQL's ROUND on NUMERIC, which rounds halves away from zero.