Run a sign test on paired before/after data with a critical-value lookup in SQL

Quick Overview

Write a SQL query that runs a sign test on paired before and after measurements: count increases and decreases, drop ties, and compare the smaller count with a seeded binomial critical value table. It tests conditional aggregation, lookup joins, and how the sign test treats ties.

Run a sign test on paired before/after data with a critical-value lookup in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

The `pairs` table has one row per subject with `before_val` and `after_val`. Run a sign test using the seeded `binomial_critical` table, which gives `critical_value` for each effective sample size `n` (the number of non-tied pairs). Return one row with: - `n_plus`: the number of pairs with `after_val` > `before_val` - `n_minus`: the number of pairs with `after_val` < `before_val` - `s_stat`: the smaller of `n_plus` and `n_minus` - `decision`: `'reject'` when `s_stat` <= `critical_value` for that `n`, else `'accept'` Ties (`after_val` = `before_val`) are ignored: they count toward neither `n_plus` nor `n_minus`, nor toward `n`. ```hint Lookup key Reduce the table to the two counts first. The statistic and the key for the critical-value lookup both follow from those two numbers. ``` ### Input Table(s) Table `pairs` | Column | Type | Meaning | |---|---|---| | before_val | INT | The subject's measurement before the change | | after_val | INT | The same subject's measurement after the change | Each row is one subject. The table has no primary key. Table `binomial_critical` | Column | Type | Meaning | |---|---|---| | n | INT | Effective sample size (number of non-tied pairs) | | critical_value | INT | Largest `s_stat` that still rejects at this `n`; -1 means no value of `s_stat` rejects | `n` is the primary key. ### Output Contract - Exactly one row with the columns `n_plus`, `n_minus`, `s_stat`, `decision`, in that order. - `n_plus`, `n_minus` and `s_stat` are integers. - `decision` is the text `'reject'` or `'accept'`. ### Example Input `pairs`: | before_val | after_val | |---|---| | 1 | 6 | | 4 | 3 | | 7 | 7 | | 6 | 10 | | 3 | 6 | | 1 | 9 | | 6 | 5 | | 8 | 5 | Input `binomial_critical`: | n | critical_value | |---|---| | 1 | -1 | | 2 | -1 | | 3 | 0 | | 4 | 0 | | 5 | 0 | | 6 | 0 | | 7 | 0 | | 8 | 0 | | 9 | 1 | | 10 | 1 | | 11 | 1 | | 12 | 2 | | 13 | 2 | | 14 | 2 | | 15 | 3 | Expected output: | n_plus | n_minus | s_stat | decision | |---|---|---|---| | 4 | 3 | 3 | accept | Four pairs went up, three went down, and (7, 7) is a tie, so $n = 7$. The critical value for $n = 7$ is 0, and 3 is greater than 0, so the decision is `'accept'`. ### Constraints and Clarifications - `before_val` and `after_val` are never NULL. - At least one pair is not tied, and `binomial_critical` has a row for the resulting `n`. - Use the critical values exactly as seeded; do not compute your own.

Overview: Write a SQL query that runs a sign test on paired before and after measurements: count increases and decreases, drop ties, and compare the smaller count with a seeded binomial critical value table. It tests conditional aggregation, lookup joins, and how the sign test treats ties.

General logo
General
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

The pairs table has one row per subject with before_val and after_val. Run a sign test using the seeded binomial_critical table, which gives critical_value for each effective sample size n (the number of non-tied pairs). Return one row with:

  • n_plus : the number of pairs with after_val > before_val
  • n_minus : the number of pairs with after_val < before_val
  • s_stat : the smaller of n_plus and n_minus
  • decision : 'reject' when s_stat <= critical_value for that n , else 'accept'

Ties (after_val = before_val) are ignored: they count toward neither n_plus nor n_minus, nor toward n.

Input Table(s)

Table pairs

ColumnTypeMeaning
before_valINTThe subject's measurement before the change
after_valINTThe same subject's measurement after the change

Each row is one subject. The table has no primary key.

Table binomial_critical

ColumnTypeMeaning
nINTEffective sample size (number of non-tied pairs)
critical_valueINTLargest s_stat that still rejects at this n; -1 means no value of s_stat rejects

n is the primary key.

Output Contract

  • Exactly one row with the columns n_plus , n_minus , s_stat , decision , in that order.
  • n_plus , n_minus and s_stat are integers.
  • decision is the text 'reject' or 'accept' .

Example

Input pairs:

before_valafter_val
16
43
77
610
36
19
65
85

Input binomial_critical:

ncritical_value
1-1
2-1
30
40
50
60
70
80
91
101
111
122
132
142
153

Expected output:

n_plusn_minuss_statdecision
433accept

Four pairs went up, three went down, and (7, 7) is a tie, so n=7n = 7. The critical value for n=7n = 7 is 0, and 3 is greater than 0, so the decision is 'accept'.

Constraints and Clarifications

  • before_val and after_val are never NULL.
  • At least one pair is not tied, and binomial_critical has a row for the resulting n .
  • Use the critical values exactly as seeded; do not compute your own.