Wald Confidence Interval for a Conversion Rate

Quick Overview

Write a SQL query that computes a conversion rate and its 95% Wald confidence interval from a table of 0/1 conversion flags. It tests aggregation, the standard error of a proportion, avoiding integer division, and controlling numeric types and rounding in PostgreSQL.

Wald Confidence Interval for a Conversion Rate

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Table `trials` has one row per user with a `converted` flag (1 if the user converted, 0 if not). Compute a 95% Wald confidence interval for the overall conversion rate. With $n$ users and $x$ conversions, the point estimate is $\hat{p} = x / n$ and the Wald standard error is $SE = \sqrt{\hat{p}(1 - \hat{p}) / n}$. The interval runs from $\hat{p} - 1.96 \cdot SE$ to $\hat{p} + 1.96 \cdot SE$. Return one row with `p_hat`, `ci_lower` and `ci_upper`, each rounded to 4 decimals. ```hint Check the types `COUNT` and `SUM` both return integers. Look at what type your division produces before you take the square root. ``` ### Input Table(s) Table `trials` | Column | Type | Meaning | |---|---|---| | converted | INTEGER | 1 if the user converted, 0 if not | Each row is one user. The table has no key column. ### Output Contract Exactly one row with these columns, in this order: | Column | Meaning | |---|---| | p_hat | $x / n$, rounded to 4 decimals | | ci_lower | $\hat{p} - 1.96 \cdot SE$, rounded to 4 decimals | | ci_upper | $\hat{p} + 1.96 \cdot SE$, rounded to 4 decimals | Compute all three values from the unrounded $\hat{p}$ and $SE$, and round only the final results. The result has one row, so it needs no ordering. ### Example Input `trials` (12 rows): | converted | |---| | 0 | | 0 | | 0 | | 1 | | 0 | | 0 | | 1 | | 0 | | 1 | | 1 | | 0 | | 1 | Expected output: | p_hat | ci_lower | ci_upper | |---|---|---| | 0.4167 | 0.1377 | 0.6956 | Five of the 12 users converted, so $\hat{p} = 5/12 \approx 0.416667$. Then $SE = \sqrt{(5/12)(7/12)/12} \approx 0.142319$ and ${1.96 \cdot SE \approx 0.278945}$, which puts the bounds at about 0.137722 and 0.695611. ### Constraints and Clarifications - `converted` is never NULL and is always 0 or 1. - The table has at least one row. - Use 1.96 as the critical value. - Do not clip the bounds to the range 0 to 1. With a small $n$ or a rate near 0 or 1, a Wald bound can fall outside that range; return it as computed. - If no user converted, or every user did, $SE$ is 0 and both bounds equal `p_hat`.

Overview: Write a SQL query that computes a conversion rate and its 95% Wald confidence interval from a table of 0/1 conversion flags. It tests aggregation, the standard error of a proportion, avoiding integer division, and controlling numeric types and rounding in PostgreSQL.

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

Table trials has one row per user with a converted flag (1 if the user converted, 0 if not). Compute a 95% Wald confidence interval for the overall conversion rate.

With nn users and xx conversions, the point estimate is p^=x/n\hat{p} = x / n and the Wald standard error is SE=p^(1−p^)/nSE = \sqrt{\hat{p}(1 - \hat{p}) / n}. The interval runs from p^−1.96⋅SE\hat{p} - 1.96 \cdot SE to p^+1.96⋅SE\hat{p} + 1.96 \cdot SE. Return one row with p_hat, ci_lower and ci_upper, each rounded to 4 decimals.

Input Table(s)

Table trials

ColumnTypeMeaning
convertedINTEGER1 if the user converted, 0 if not

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

Output Contract

Exactly one row with these columns, in this order:

ColumnMeaning
p_hatx/nx / n, rounded to 4 decimals
ci_lowerp^−1.96⋅SE\hat{p} - 1.96 \cdot SE, rounded to 4 decimals
ci_upperp^+1.96⋅SE\hat{p} + 1.96 \cdot SE, rounded to 4 decimals

Compute all three values from the unrounded p^\hat{p} and SESE, and round only the final results. The result has one row, so it needs no ordering.

Example

Input trials (12 rows):

converted
0
0
0
1
0
0
1
0
1
1
0
1

Expected output:

p_hatci_lowerci_upper
0.41670.13770.6956

Five of the 12 users converted, so p^=5/12≈0.416667\hat{p} = 5/12 \approx 0.416667. Then SE=(5/12)(7/12)/12≈0.142319SE = \sqrt{(5/12)(7/12)/12} \approx 0.142319 and 1.96⋅SE≈0.278945{1.96 \cdot SE \approx 0.278945}, which puts the bounds at about 0.137722 and 0.695611.

Constraints and Clarifications

  • converted is never NULL and is always 0 or 1.
  • The table has at least one row.
  • Use 1.96 as the critical value.
  • Do not clip the bounds to the range 0 to 1. With a small nn or a rate near 0 or 1, a Wald bound can fall outside that range; return it as computed.
  • If no user converted, or every user did, SESE is 0 and both bounds equal p_hat .