Compute the Sample Variance of Each A/B Test Group in One SQL Query

Quick Overview

Write a SQL query that returns the sample variance of a metric for experiment groups A and B side by side in a single row. It tests conditional aggregation, how aggregate functions skip NULLs, and the difference between sample and population variance.

Compute the Sample Variance of Each A/B Test Group in One SQL Query

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `metrics` table records a numeric `value` per user with a `grp` label of `'A'` or `'B'`. Using conditional aggregation, return one row with `variance_a` and `variance_b`: the sample variance (`VAR_SAMP`) of `value` for each group, rounded to 4 decimals. ```hint One row, two groups Grouping by `grp` gives one row per group. Think about how to put both groups' numbers into columns of a single row from one scan of the table. ``` ### Input Table(s) `metrics` | column | type | meaning | |---|---|---| | grp | VARCHAR(5) | Experiment group label, `'A'` or `'B'` | | value | DECIMAL(10,4) | The user's measured metric | The table has no declared key. Each row is one user's measurement. ### Output Contract - Exactly one row with two columns, in this order: `variance_a`, `variance_b`. - Each column is the sample variance (denominator $n - 1$) of `value` for that group, rounded to 4 decimal places. - The result has a single row, so no ordering is needed. ### Example Input `metrics`: | grp | value | |---|---| | A | 10.7 | | B | 38.2 | | A | 17.5 | | A | 14.1 | | B | 23.9 | | B | 18.4 | | A | 19.8 | | B | 5.1 | Expected output: | variance_a | variance_b | |---|---| | 15.8292 | 187.7267 | Group A has mean 15.525 and squared deviations summing to 47.4875, so its sample variance is 47.4875 / 3, about 15.8292. Group B has mean 21.4 and squared deviations summing to 563.18, so its sample variance is 563.18 / 3, about 187.7267. ### Constraints and Clarifications - Rows with a NULL `value` are ignored. - A group with fewer than two non-NULL values has no sample variance, so its column is NULL. - Rows with any `grp` label other than `'A'` or `'B'` do not count toward either column. - Duplicate rows are separate observations and all count. - If the table is empty, the result is still one row, with both columns NULL.

Overview: Write a SQL query that returns the sample variance of a metric for experiment groups A and B side by side in a single row. It tests conditional aggregation, how aggregate functions skip NULLs, and the difference between sample and population variance.

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

The metrics table records a numeric value per user with a grp label of 'A' or 'B'. Using conditional aggregation, return one row with variance_a and variance_b: the sample variance (VAR_SAMP) of value for each group, rounded to 4 decimals.

Input Table(s)

metrics

columntypemeaning
grpVARCHAR(5)Experiment group label, 'A' or 'B'
valueDECIMAL(10,4)The user's measured metric

The table has no declared key. Each row is one user's measurement.

Output Contract

  • Exactly one row with two columns, in this order: variance_a , variance_b .
  • Each column is the sample variance (denominator n−1n - 1 ) of value for that group, rounded to 4 decimal places.
  • The result has a single row, so no ordering is needed.

Example

Input metrics:

grpvalue
A10.7
B38.2
A17.5
A14.1
B23.9
B18.4
A19.8
B5.1

Expected output:

variance_avariance_b
15.8292187.7267

Group A has mean 15.525 and squared deviations summing to 47.4875, so its sample variance is 47.4875 / 3, about 15.8292. Group B has mean 21.4 and squared deviations summing to 563.18, so its sample variance is 563.18 / 3, about 187.7267.

Constraints and Clarifications

  • Rows with a NULL value are ignored.
  • A group with fewer than two non-NULL values has no sample variance, so its column is NULL.
  • Rows with any grp label other than 'A' or 'B' do not count toward either column.
  • Duplicate rows are separate observations and all count.
  • If the table is empty, the result is still one row, with both columns NULL.