Pooled-Variance Two-Sample t-Statistic and Degrees of Freedom in SQL

Quick Overview

Write a SQL query that computes Student's pooled-variance two-sample t-statistic comparing control and treatment means, along with its degrees of freedom. It tests conditional aggregation, the sample variance, pooling by degrees of freedom and numeric types in PostgreSQL.

Pooled-Variance Two-Sample t-Statistic and Degrees of Freedom in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Using table `measurements`, compute Student's pooled-variance two-sample t-statistic, with `'control'` as group 1 and `'treatment'` as group 2: $$ s_p^2 = \frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}, \qquad t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_p^2 \left(\frac{1}{n_1} + \frac{1}{n_2}\right)}} $$ Here $n_i$ is the number of values in group $i$, $\bar{x}_i$ is its mean and $s_i^2$ is its sample variance. Return one row with `t_statistic`, rounded to 4 decimals, and `df`, equal to $n_1 + n_2 - 2$. ```hint Sign and types The sign of the statistic follows the order of subtraction, and the task fixes control as group 1. Keep the arithmetic in numeric types so that `1 / n` does not truncate to zero. ``` ### Input Table(s) `measurements` | Column | Type | Meaning | |---|---|---| | group_name | VARCHAR(20) | `'control'` or `'treatment'` | | value | DECIMAL(10,4) | The continuous measurement | There is no primary key. Each row is one measurement, and two rows with the same value are two separate observations. ### Output Contract Exactly one row with these columns, in this order: | Column | Meaning | |---|---| | t_statistic | The pooled two-sample t-statistic, control minus treatment, rounded to 4 decimals | | df | Degrees of freedom $n_1 + n_2 - 2$, as an integer | Round only the final `t_statistic`. The result is a single row, so no ordering applies. ### Example `measurements` | group_name | value | |---|---| | treatment | 7.4 | | control | 5.7 | | treatment | 6.1 | | control | 5.1 | | treatment | 6.3 | | treatment | 7.8 | | control | 6 | | treatment | 7 | | control | 4.8 | | treatment | 6.5 | Expected result: | t_statistic | df | |---|---| | -3.5999 | 8 | Control has 4 values with mean 5.4 and sample variance 0.3. Treatment has 6 values with mean 6.85 and sample variance 0.443. The pooled variance is ${(3 \cdot 0.3 + 5 \cdot 0.443) / 8 = 0.389375}$, the standard error is ${\sqrt{0.389375 \cdot (1/4 + 1/6)} \approx 0.4028}$, and the statistic is ${-1.45 / 0.4028 \approx -3.5999}$ with ${4 + 6 - 2 = 8}$ degrees of freedom. ### Constraints and Clarifications - Each group has at least 2 non-NULL values, and the pooled variance is positive, so the statistic is defined. - A row whose `value` is NULL is ignored by every per-group statistic, including the counts that feed `df`. - Duplicate rows are separate measurements. Do not deduplicate them. - `group_name` holds only the two lowercase labels `'control'` and `'treatment'`. - The variances are sample variances, with $n - 1$ in the denominator.

Overview: Write a SQL query that computes Student's pooled-variance two-sample t-statistic comparing control and treatment means, along with its degrees of freedom. It tests conditional aggregation, the sample variance, pooling by degrees of freedom and numeric types in PostgreSQL.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Using table measurements, compute Student's pooled-variance two-sample t-statistic, with 'control' as group 1 and 'treatment' as group 2:

sp2=(n1−1)s12+(n2−1)s22n1+n2−2,t=xˉ1−xˉ2sp2(1n1+1n2)s_p^2 = \frac{(n_1 - 1)s_1^2 + (n_2 - 1)s_2^2}{n_1 + n_2 - 2}, \qquad t = \frac{\bar{x}_1 - \bar{x}_2}{\sqrt{s_p^2 \left(\frac{1}{n_1} + \frac{1}{n_2}\right)}}

Here nin_i is the number of values in group ii, xˉi\bar{x}_i is its mean and si2s_i^2 is its sample variance. Return one row with t_statistic, rounded to 4 decimals, and df, equal to n1+n2−2n_1 + n_2 - 2.

Input Table(s)

measurements

ColumnTypeMeaning
group_nameVARCHAR(20)'control' or 'treatment'
valueDECIMAL(10,4)The continuous measurement

There is no primary key. Each row is one measurement, and two rows with the same value are two separate observations.

Output Contract

Exactly one row with these columns, in this order:

ColumnMeaning
t_statisticThe pooled two-sample t-statistic, control minus treatment, rounded to 4 decimals
dfDegrees of freedom n1+n2−2n_1 + n_2 - 2, as an integer

Round only the final t_statistic. The result is a single row, so no ordering applies.

Example

measurements

group_namevalue
treatment7.4
control5.7
treatment6.1
control5.1
treatment6.3
treatment7.8
control6
treatment7
control4.8
treatment6.5

Expected result:

t_statisticdf
-3.59998

Control has 4 values with mean 5.4 and sample variance 0.3. Treatment has 6 values with mean 6.85 and sample variance 0.443. The pooled variance is (3⋅0.3+5⋅0.443)/8=0.389375{(3 \cdot 0.3 + 5 \cdot 0.443) / 8 = 0.389375}, the standard error is 0.389375⋅(1/4+1/6)≈0.4028{\sqrt{0.389375 \cdot (1/4 + 1/6)} \approx 0.4028}, and the statistic is −1.45/0.4028≈−3.5999{-1.45 / 0.4028 \approx -3.5999} with 4+6−2=8{4 + 6 - 2 = 8} degrees of freedom.

Constraints and Clarifications

  • Each group has at least 2 non-NULL values, and the pooled variance is positive, so the statistic is defined.
  • A row whose value is NULL is ignored by every per-group statistic, including the counts that feed df .
  • Duplicate rows are separate measurements. Do not deduplicate them.
  • group_name holds only the two lowercase labels 'control' and 'treatment' .
  • The variances are sample variances, with n−1n - 1 in the denominator.