Compute relative risk and risk difference from a 2x2 cohort table in SQL

Quick Overview

Write a PostgreSQL query that turns subject-level cohort rows into the risk of an event in the exposed and unexposed groups, then returns their ratio and difference rounded to 4 decimals. It tests conditional aggregation, exact division, NULL handling for empty groups and reading risk measures.

Compute relative risk and risk difference from a 2x2 cohort table in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Table `cohort` has one row per subject, with a `group_name` (`'exposed'` or `'unexposed'`) and an `outcome` flag (1 = event, 0 = no event). Compute each group's risk as its number of events divided by its number of subjects: $$p_e = \frac{x_e}{n_e}, \qquad p_u = \frac{x_u}{n_u}$$ Return one row with `relative_risk` $= p_e / p_u$ and `risk_difference` $= p_e - p_u$, each rounded to 4 decimals. ```hint Two counts per group Each group needs a subject count and an event count. Check what your query does when the unexposed group has no events. ``` ### Input Table(s) `cohort` | Column | Type | Meaning | |---|---|---| | `group_name` | VARCHAR(20) | 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 two columns, in this order: `relative_risk`, `risk_difference`. - `relative_risk` is $p_e / p_u$ rounded to 4 decimal places. It is NULL when either group has no subjects or when the unexposed group has no events. - `risk_difference` is $p_e - p_u$ rounded to 4 decimal places. It is NULL when either group has no subjects, and it can be negative. ### Example `cohort` (21 rows): | group_name | outcome | |---|---| | exposed | 0 | | unexposed | 0 | | unexposed | 1 | | exposed | 1 | | unexposed | 0 | | exposed | 1 | | unexposed | 0 | | unexposed | 0 | | unexposed | 1 | | unexposed | 1 | | exposed | 1 | | exposed | 0 | | exposed | 0 | | unexposed | 0 | | unexposed | 1 | | exposed | 0 | | exposed | 0 | | exposed | 1 | | exposed | 1 | | unexposed | 0 | | unexposed | 0 | Expected result: | relative_risk | risk_difference | |---|---| | 1.3750 | 0.1364 | The exposed group has 5 events among 10 subjects ($p_e = 0.5$) and the unexposed group has 4 events among 11 subjects ($p_u \approx 0.3636$). So the relative risk is ${0.5 / (4/11) = 55/40 = 1.375}$ and the risk difference is ${0.5 - 4/11 = 15/110 \approx 0.1364}$. ### Constraints and Clarifications - `group_name` 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. - Compute both outputs from the exact risks and round only the final values. - If the table is empty, return one row with both columns NULL.

Overview: Write a PostgreSQL query that turns subject-level cohort rows into the risk of an event in the exposed and unexposed groups, then returns their ratio and difference rounded to 4 decimals. It tests conditional aggregation, exact division, NULL handling for empty groups and reading risk measures.

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

Table cohort has one row per subject, with a group_name ('exposed' or 'unexposed') and an outcome flag (1 = event, 0 = no event). Compute each group's risk as its number of events divided by its number of subjects:

pe=xene,pu=xunup_e = \frac{x_e}{n_e}, \qquad p_u = \frac{x_u}{n_u}

Return one row with relative_risk =pe/pu= p_e / p_u and risk_difference =pe−pu= p_e - p_u, each rounded to 4 decimals.

Input Table(s)

cohort

ColumnTypeMeaning
group_nameVARCHAR(20)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 two columns, in this order: relative_risk , risk_difference .
  • relative_risk is pe/pup_e / p_u rounded to 4 decimal places. It is NULL when either group has no subjects or when the unexposed group has no events.
  • risk_difference is pe−pup_e - p_u rounded to 4 decimal places. It is NULL when either group has no subjects, and it can be negative.

Example

cohort (21 rows):

group_nameoutcome
exposed0
unexposed0
unexposed1
exposed1
unexposed0
exposed1
unexposed0
unexposed0
unexposed1
unexposed1
exposed1
exposed0
exposed0
unexposed0
unexposed1
exposed0
exposed0
exposed1
exposed1
unexposed0
unexposed0

Expected result:

relative_riskrisk_difference
1.37500.1364

The exposed group has 5 events among 10 subjects (pe=0.5p_e = 0.5) and the unexposed group has 4 events among 11 subjects (pu≈0.3636p_u \approx 0.3636). So the relative risk is 0.5/(4/11)=55/40=1.375{0.5 / (4/11) = 55/40 = 1.375} and the risk difference is 0.5−4/11=15/110≈0.1364{0.5 - 4/11 = 15/110 \approx 0.1364}.

Constraints and Clarifications

  • group_name 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.
  • Compute both outputs from the exact risks and round only the final values.
  • If the table is empty, return one row with both columns NULL.