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
| 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).