One-Proportion Z-Test Against a Baseline

Quick Overview

Compute a one-proportion z-test in SQL from a table of 0/1 conversion flags, comparing the observed conversion rate with a 0.5 baseline. It tests aggregation, avoiding integer division, using the standard error under the null hypothesis, and turning a test statistic into a significance flag.

One-Proportion Z-Test Against a Baseline

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/0). Run a one-proportion z-test against the baseline $p_0 = 0.5$. With $n$ users and $x$ conversions: $$\hat p = \frac{x}{n}, \qquad z = \frac{\hat p - 0.5}{\sqrt{0.5 \cdot (1 - 0.5) / n}}$$ Return one row with `z_statistic` rounded to 4 decimals and `significant` (1 when $|z| > 1.96$, else 0). ```hint Watch the arithmetic types Get n and x from one aggregate first. If both are integers, PostgreSQL's x / n drops the fraction. ``` ### Input Table(s) Table `trials` | Column | Type | Meaning | |---|---|---| | `converted` | SMALLINT | 1 if the user converted, 0 if not | Keys: the table has no key column. Each row is one user. ### Output Contract Exactly one row with these columns, in this order: | Column | Type | Meaning | |---|---|---| | `z_statistic` | NUMERIC | the z statistic rounded to 4 decimal places | | `significant` | INTEGER | 1 when the unrounded $|z|$ is greater than 1.96, else 0 | The result is a single row, so no ordering is needed. ### Example Input `trials` (14 rows): | converted | |---| | 0 | | 1 | | 0 | | 0 | | 0 | | 1 | | 0 | | 0 | | 0 | | 1 | | 0 | | 1 | | 0 | | 1 | Expected output: | z_statistic | significant | |---|---| | -1.0690 | 0 | Here $n = 14$ and $x = 5$, so $\hat p = 5/14 \approx 0.3571$. The standard error under the baseline is $\sqrt{0.25/14} \approx 0.1336$, which gives $z \approx -0.1429 / 0.1336 \approx -1.0690$. Since $|z| < 1.96$, the flag is 0. ### Constraints and Clarifications - `trials` has at least one row, so $n > 0$. - `converted` is always 0 or 1 and is never NULL. - The standard error uses the baseline rate 0.5, not the observed rate. - `significant` is computed from the unrounded z. The comparison is strict, so a value of exactly 1.96 is not significant. - Round `z_statistic` with PostgreSQL `ROUND(numeric, 4)`.

Overview: Compute a one-proportion z-test in SQL from a table of 0/1 conversion flags, comparing the observed conversion rate with a 0.5 baseline. It tests aggregation, avoiding integer division, using the standard error under the null hypothesis, and turning a test statistic into a significance flag.

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/0). Run a one-proportion z-test against the baseline p0=0.5p_0 = 0.5. With nn users and xx conversions:

p^=xn,z=p^−0.50.5⋅(1−0.5)/n\hat p = \frac{x}{n}, \qquad z = \frac{\hat p - 0.5}{\sqrt{0.5 \cdot (1 - 0.5) / n}}

Return one row with z_statistic rounded to 4 decimals and significant (1 when ∣z∣>1.96|z| > 1.96, else 0).

Input Table(s)

Table trials

ColumnTypeMeaning
convertedSMALLINT1 if the user converted, 0 if not

Keys: the table has no key column. Each row is one user.

Output Contract

Exactly one row with these columns, in this order:

ColumnTypeMeaning
z_statisticNUMERICthe z statistic rounded to 4 decimal places
significantINTEGER1 when the unrounded $

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

Example

Input trials (14 rows):

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

Expected output:

z_statisticsignificant
-1.06900

Here n=14n = 14 and x=5x = 5, so p^=5/14≈0.3571\hat p = 5/14 \approx 0.3571. The standard error under the baseline is 0.25/14≈0.1336\sqrt{0.25/14} \approx 0.1336, which gives z≈−0.1429/0.1336≈−1.0690z \approx -0.1429 / 0.1336 \approx -1.0690. Since ∣z∣<1.96|z| < 1.96, the flag is 0.

Constraints and Clarifications

  • trials has at least one row, so n>0n > 0 .
  • converted is always 0 or 1 and is never NULL.
  • The standard error uses the baseline rate 0.5, not the observed rate.
  • significant is computed from the unrounded z. The comparison is strict, so a value of exactly 1.96 is not significant.
  • Round z_statistic with PostgreSQL ROUND(numeric, 4) .