Cohen's d with Pooled Standard Deviation

Quick Overview

Write a SQL query that computes Cohen's d for a treatment group against a control group, using the pooled standard deviation of the two groups. It tests conditional aggregation, sample variance functions, and turning group statistics into an effect size.

Cohen's d with Pooled Standard Deviation

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `measurements` has one row per subject, with a `group_name` (`'control'` or `'treatment'`) and a continuous `value`. Compute Cohen's d for treatment versus control using the pooled standard deviation. Return one row with `cohens_d` rounded to 4 decimals. Let m1, v1 and n1 be the control group's mean, sample variance and size, and m2, v2 and n2 the same for treatment. Then: - `pooled_sd = sqrt(((n1 - 1) * v1 + (n2 - 1) * v2) / (n1 + n2 - 2))` - `cohens_d = (m2 - m1) / pooled_sd` ```hint Which variance The formula uses each group's sample variance. Check whether the SQL variance function you reach for divides by n or by n - 1. ``` ### Input Table(s) Table `measurements`: | Column | Type | Meaning | |---|---|---| | `group_name` | VARCHAR(20) | `'control'` or `'treatment'` | | `value` | DECIMAL(10,2) | The subject's measurement | The table has no primary key. Each row is one subject, and two subjects can have the same value. ### Output Contract - Exactly one row with one column, `cohens_d`. - `cohens_d` is `(m2 - m1) / pooled_sd` as defined above, rounded to 4 decimal places. - The sign follows treatment minus control, so the value is positive when the treatment mean is higher. ### Example Input `measurements`: | group_name | value | |---|---| | control | 11.7 | | treatment | 17.25 | | treatment | 16.19 | | treatment | 14.43 | | control | 13.73 | | control | 11.35 | | treatment | 17.94 | | control | 12.64 | | control | 13.39 | | control | 10.36 | | control | 12.55 | | treatment | 16.86 | | treatment | 16.76 | | treatment | 14.32 | | control | 11.24 | | treatment | 13.83 | Expected output: | cohens_d | |---| | 2.8110 | Control has 8 values with mean 12.12 and treatment has 8 values with mean 15.9475. The pooled standard deviation is about 1.36159, so d is 3.8275 / 1.36159, about 2.81104, which rounds to 2.8110. ### Constraints and Clarifications - `group_name` is always `'control'` or `'treatment'`, and `value` is never NULL. - Each group has at least two rows, and the pooled standard deviation is greater than zero, so the formula never divides by zero. - Rows with the same value are different subjects and all of them count. - The output is a single row, so there is no ordering or tie rule.

Overview: Write a SQL query that computes Cohen's d for a treatment group against a control group, using the pooled standard deviation of the two groups. It tests conditional aggregation, sample variance functions, and turning group statistics into an effect size.

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

Table measurements has one row per subject, with a group_name ('control' or 'treatment') and a continuous value. Compute Cohen's d for treatment versus control using the pooled standard deviation. Return one row with cohens_d rounded to 4 decimals.

Let m1, v1 and n1 be the control group's mean, sample variance and size, and m2, v2 and n2 the same for treatment. Then:

  • pooled_sd = sqrt(((n1 - 1) * v1 + (n2 - 1) * v2) / (n1 + n2 - 2))
  • cohens_d = (m2 - m1) / pooled_sd

Input Table(s)

Table measurements:

ColumnTypeMeaning
group_nameVARCHAR(20)'control' or 'treatment'
valueDECIMAL(10,2)The subject's measurement

The table has no primary key. Each row is one subject, and two subjects can have the same value.

Output Contract

  • Exactly one row with one column, cohens_d .
  • cohens_d is (m2 - m1) / pooled_sd as defined above, rounded to 4 decimal places.
  • The sign follows treatment minus control, so the value is positive when the treatment mean is higher.

Example

Input measurements:

group_namevalue
control11.7
treatment17.25
treatment16.19
treatment14.43
control13.73
control11.35
treatment17.94
control12.64
control13.39
control10.36
control12.55
treatment16.86
treatment16.76
treatment14.32
control11.24
treatment13.83

Expected output:

cohens_d
2.8110

Control has 8 values with mean 12.12 and treatment has 8 values with mean 15.9475. The pooled standard deviation is about 1.36159, so d is 3.8275 / 1.36159, about 2.81104, which rounds to 2.8110.

Constraints and Clarifications

  • group_name is always 'control' or 'treatment' , and value is never NULL.
  • Each group has at least two rows, and the pooled standard deviation is greater than zero, so the formula never divides by zero.
  • Rows with the same value are different subjects and all of them count.
  • The output is a single row, so there is no ordering or tie rule.