Compute the odds ratio from a 2x2 exposure and outcome table in SQL

Quick Overview

Write a PostgreSQL query that counts the four cells of a 2x2 exposure by outcome table from subject-level rows and returns the odds ratio rounded to 4 decimals. It tests conditional aggregation, safe division when a cell is empty, and reading an odds ratio correctly.

Compute the odds ratio from a 2x2 exposure and outcome table in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Table `exposure_log` has one row per subject, with an `exposure` group (`'exposed'` or `'unexposed'`) and an `outcome` flag (1 = event, 0 = no event). Arrange the subjects in a 2x2 table: | | event (1) | no event (0) | |---|---|---| | exposed | a | b | | unexposed | c | d | Compute the odds ratio $$\text{OR} = \frac{a \cdot d}{b \cdot c}$$ and return one row with `odds_ratio` rounded to 4 decimals. ```hint Check the small cases Each of a, b, c and d is a count over a subset of the rows. Check what your query does when b or c is 0. ``` ### Input Table(s) `exposure_log` | Column | Type | Meaning | |---|---|---| | `exposure` | VARCHAR(20) | Exposure group of the subject, `'exposed'` or `'unexposed'` | | `outcome` | SMALLINT | 1 if the subject had the event, 0 if not | The table has no key. Each row is one subject, so identical rows are normal. ### Output Contract - Exactly one row with one column, `odds_ratio`. - `odds_ratio` is ${(a \cdot d) / (b \cdot c)}$ rounded to 4 decimal places. - When ${b \cdot c = 0}$ the ratio is undefined or infinite, and `odds_ratio` is NULL. ### Example `exposure_log` (18 rows): | exposure | outcome | |---|---| | unexposed | 0 | | unexposed | 0 | | exposed | 0 | | unexposed | 1 | | unexposed | 1 | | unexposed | 1 | | unexposed | 0 | | exposed | 1 | | exposed | 1 | | exposed | 1 | | exposed | 0 | | exposed | 1 | | exposed | 0 | | exposed | 1 | | unexposed | 0 | | unexposed | 1 | | exposed | 1 | | unexposed | 1 | Expected result: | odds_ratio | |---| | 1.6000 | The cells are $a = 6$, $b = 3$, $c = 5$ and $d = 4$, so the odds ratio is ${(6 \cdot 4) / (3 \cdot 5) = 24/15 = 1.6}$. ### Constraints and Clarifications - `exposure` is never NULL and is always `'exposed'` or `'unexposed'`. `outcome` is never NULL and is always 0 or 1. - Count every row. Duplicate rows are separate subjects. - Use exact division and round only the final value. - If `a` or `d` is 0 while ${b \cdot c > 0}$, the odds ratio is 0. - If the table is empty, all four counts are 0, so return one row with NULL.

Overview: Write a PostgreSQL query that counts the four cells of a 2x2 exposure by outcome table from subject-level rows and returns the odds ratio rounded to 4 decimals. It tests conditional aggregation, safe division when a cell is empty, and reading an odds ratio correctly.

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

Table exposure_log has one row per subject, with an exposure group ('exposed' or 'unexposed') and an outcome flag (1 = event, 0 = no event). Arrange the subjects in a 2x2 table:

event (1)no event (0)
exposedab
unexposedcd

Compute the odds ratio

OR=a⋅db⋅c\text{OR} = \frac{a \cdot d}{b \cdot c}

and return one row with odds_ratio rounded to 4 decimals.

Input Table(s)

exposure_log

ColumnTypeMeaning
exposureVARCHAR(20)Exposure group of the subject, 'exposed' or 'unexposed'
outcomeSMALLINT1 if the subject had the event, 0 if not

The table has no key. Each row is one subject, so identical rows are normal.

Output Contract

  • Exactly one row with one column, odds_ratio .
  • odds_ratio is (a⋅d)/(b⋅c){(a \cdot d) / (b \cdot c)} rounded to 4 decimal places.
  • When b⋅c=0{b \cdot c = 0} the ratio is undefined or infinite, and odds_ratio is NULL.

Example

exposure_log (18 rows):

exposureoutcome
unexposed0
unexposed0
exposed0
unexposed1
unexposed1
unexposed1
unexposed0
exposed1
exposed1
exposed1
exposed0
exposed1
exposed0
exposed1
unexposed0
unexposed1
exposed1
unexposed1

Expected result:

odds_ratio
1.6000

The cells are a=6a = 6, b=3b = 3, c=5c = 5 and d=4d = 4, so the odds ratio is (6⋅4)/(3⋅5)=24/15=1.6{(6 \cdot 4) / (3 \cdot 5) = 24/15 = 1.6}.

Constraints and Clarifications

  • exposure is never NULL and is always 'exposed' or 'unexposed' . outcome is never NULL and is always 0 or 1.
  • Count every row. Duplicate rows are separate subjects.
  • Use exact division and round only the final value.
  • If a or d is 0 while b⋅c>0{b \cdot c > 0} , the odds ratio is 0.
  • If the table is empty, all four counts are 0, so return one row with NULL.