F-Test for Equal Variances in SQL

Quick Overview

Write a SQL query that runs an F-test for equal variances on two paired measurement columns, reporting the variance ratio, its degrees of freedom, the matching critical value from a lookup table and a significance flag. It tests aggregate functions, a lookup join and how the variance-ratio test works.

F-Test for Equal Variances in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

The `samples` table holds n paired measurements in columns `x` and `y`. The `f_critical_values` table supplies F critical values keyed by degrees of freedom. Write a query that tests whether the two columns have equal variance and returns one row with the F statistic, its degrees of freedom, the critical value and a significance flag. ```hint Ordering the ratio You do not need a CASE expression to decide which variance goes on top. Look for functions that return the larger or the smaller of two values. ``` ### Input Table(s) `samples` | Column | Type | Meaning | |---|---|---| | x | DECIMAL(10,4) | First measurement of the pair | | y | DECIMAL(10,4) | Second measurement of the pair | There is no primary key. Each row is one paired observation. `f_critical_values` | Column | Type | Meaning | |---|---|---| | df | INT | Degrees of freedom, used for both numerator and denominator | | critical_value | DECIMAL(8,4) | F critical value for (df, df) degrees of freedom | Primary key: `df`. ### Output Contract Exactly one row with these columns, in this order: | Column | Definition | |---|---| | f_statistic | The larger sample variance divided by the smaller one (so it is at least 1), rounded to 4 decimals | | df_num | n-1 | | df_den | n-1 | | critical_value | The `critical_value` from `f_critical_values` where `df` = n-1, as stored | | significant | 1 when the unrounded variance ratio is greater than `critical_value`, else 0 | Sample variance uses the divisor n-1. The result is a single row, so no ordering applies. ### Example `samples` | x | y | |---|---| | 8.3 | 25.5 | | 15 | 21.9 | | 22.8 | 25 | | 8.3 | 23.7 | | 5.7 | 21.9 | | 29.3 | 18.9 | | 34.5 | 22.5 | | 34.3 | 24.6 | | 16.2 | 21.9 | `f_critical_values` (rows for df 5 to 10 shown; the full table continues to df 25) | df | critical_value | |---|---| | 5 | 7.1464 | | 6 | 5.8198 | | 7 | 4.9949 | | 8 | 4.4333 | | 9 | 4.026 | | 10 | 3.7168 | Expected result: | f_statistic | df_num | df_den | critical_value | significant | |---|---|---|---|---| | 30.2253 | 8 | 8 | 4.4333 | 1 | With 9 rows, df is 8. The variance of `x` is about 127.91 and the variance of `y` is about 4.23, so the ratio is about 30.2253, well above 4.4333. ### Constraints and Clarifications - n is the number of rows in `samples` and is at least 2. - `x` and `y` are non-NULL in every row. - Neither column is constant, so both sample variances are positive. - `f_critical_values` always contains a row for df = n-1. - Duplicate rows in `samples` are separate observations and all count. - A ratio exactly equal to the critical value is not significant (the comparison is strictly greater than).

Overview: Write a SQL query that runs an F-test for equal variances on two paired measurement columns, reporting the variance ratio, its degrees of freedom, the matching critical value from a lookup table and a significance flag. It tests aggregate functions, a lookup join and how the variance-ratio test works.

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

The samples table holds n paired measurements in columns x and y. The f_critical_values table supplies F critical values keyed by degrees of freedom. Write a query that tests whether the two columns have equal variance and returns one row with the F statistic, its degrees of freedom, the critical value and a significance flag.

Input Table(s)

samples

ColumnTypeMeaning
xDECIMAL(10,4)First measurement of the pair
yDECIMAL(10,4)Second measurement of the pair

There is no primary key. Each row is one paired observation.

f_critical_values

ColumnTypeMeaning
dfINTDegrees of freedom, used for both numerator and denominator
critical_valueDECIMAL(8,4)F critical value for (df, df) degrees of freedom

Primary key: df.

Output Contract

Exactly one row with these columns, in this order:

ColumnDefinition
f_statisticThe larger sample variance divided by the smaller one (so it is at least 1), rounded to 4 decimals
df_numn-1
df_denn-1
critical_valueThe critical_value from f_critical_values where df = n-1, as stored
significant1 when the unrounded variance ratio is greater than critical_value, else 0

Sample variance uses the divisor n-1. The result is a single row, so no ordering applies.

Example

samples

xy
8.325.5
1521.9
22.825
8.323.7
5.721.9
29.318.9
34.522.5
34.324.6
16.221.9

f_critical_values (rows for df 5 to 10 shown; the full table continues to df 25)

dfcritical_value
57.1464
65.8198
74.9949
84.4333
94.026
103.7168

Expected result:

f_statisticdf_numdf_dencritical_valuesignificant
30.2253884.43331

With 9 rows, df is 8. The variance of x is about 127.91 and the variance of y is about 4.23, so the ratio is about 30.2253, well above 4.4333.

Constraints and Clarifications

  • n is the number of rows in samples and is at least 2.
  • x and y are non-NULL in every row.
  • Neither column is constant, so both sample variances are positive.
  • f_critical_values always contains a row for df = n-1.
  • Duplicate rows in samples are separate observations and all count.
  • A ratio exactly equal to the critical value is not significant (the comparison is strictly greater than).