Compute weekly 95% margins of error for conversion rates from a z-critical lookup table in SQL

Quick Overview

Write a SQL query that computes the 95% margin of error of each week's conversion rate, reading the z critical value from a lookup table. It tests joining a parameter table, avoiding integer division, and applying the standard error of a proportion.

Compute weekly 95% margins of error for conversion rates from a z-critical lookup table in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Table `weekly` has one row per `week` with `conversions` and `visitors`. Table `z_critical` maps each `confidence` level to a `z_value`. For each `week`, compute the 95% margin of error of the conversion rate, $$ \text{margin of error} = z \sqrt{\frac{\hat{p}(1 - \hat{p})}{\text{visitors}}} $$ with $\hat{p}$ = `conversions / visitors` and $z$ taken from the `z_critical` row where `confidence = 0.95`. Return one row per `week` with `week` and `margin_of_error` rounded to 4 decimals. ```hint Check the types Look at what type `conversions / visitors` has when both columns are integers. ``` ### Input Table(s) `weekly` | Column | Type | Meaning | |---|---|---| | week | INT | Week number | | conversions | INT | Visitors who converted that week | | visitors | INT | Visitors that week | Primary key: `week`. `z_critical` | Column | Type | Meaning | |---|---|---| | confidence | DECIMAL(4,3) | Two-sided confidence level | | z_value | DECIMAL(8,4) | Standard normal critical value for that level | Primary key: `confidence`. ### Output Contract Return one row per `week` with these columns, in this order: | Column | Meaning | |---|---| | week | The week number | | margin_of_error | The 95% margin of error, rounded to 4 decimal places | Order the rows by `week` ascending. ### Example `weekly` | week | conversions | visitors | |---|---|---| | 18 | 21 | 202 | | 50 | 62 | 363 | | 36 | 24 | 365 | | 27 | 70 | 355 | | 41 | 23 | 204 | | 8 | 40 | 309 | `z_critical` | confidence | z_value | |---|---| | 0.9 | 1.6449 | | 0.95 | 1.96 | | 0.99 | 2.5758 | Expected output: | week | margin_of_error | |---|---| | 8 | 0.0374 | | 18 | 0.0421 | | 27 | 0.0414 | | 36 | 0.0254 | | 41 | 0.0434 | | 50 | 0.0387 | For week 18, $\hat{p} = 21/202 \approx 0.10396$ and the standard error is about 0.021474. Multiplying by the 0.95 row's z value of 1.96 gives about 0.0421. ### Constraints and Clarifications - `visitors` is at least 1, and `conversions` is between 0 and `visitors`. - No column is NULL. - `z_critical` has exactly one row with `confidence = 0.95`. Rows for other levels must not affect the result. - Use the formula as given, with no continuity correction.

Overview: Write a SQL query that computes the 95% margin of error of each week's conversion rate, reading the z critical value from a lookup table. It tests joining a parameter table, avoiding integer division, and applying the standard error of a proportion.

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

Table weekly has one row per week with conversions and visitors. Table z_critical maps each confidence level to a z_value. For each week, compute the 95% margin of error of the conversion rate,

margin of error=zp^(1−p^)visitors\text{margin of error} = z \sqrt{\frac{\hat{p}(1 - \hat{p})}{\text{visitors}}}

with p^\hat{p} = conversions / visitors and zz taken from the z_critical row where confidence = 0.95. Return one row per week with week and margin_of_error rounded to 4 decimals.

Input Table(s)

weekly

ColumnTypeMeaning
weekINTWeek number
conversionsINTVisitors who converted that week
visitorsINTVisitors that week

Primary key: week.

z_critical

ColumnTypeMeaning
confidenceDECIMAL(4,3)Two-sided confidence level
z_valueDECIMAL(8,4)Standard normal critical value for that level

Primary key: confidence.

Output Contract

Return one row per week with these columns, in this order:

ColumnMeaning
weekThe week number
margin_of_errorThe 95% margin of error, rounded to 4 decimal places

Order the rows by week ascending.

Example

weekly

weekconversionsvisitors
1821202
5062363
3624365
2770355
4123204
840309

z_critical

confidencez_value
0.91.6449
0.951.96
0.992.5758

Expected output:

weekmargin_of_error
80.0374
180.0421
270.0414
360.0254
410.0434
500.0387

For week 18, p^=21/202≈0.10396\hat{p} = 21/202 \approx 0.10396 and the standard error is about 0.021474. Multiplying by the 0.95 row's z value of 1.96 gives about 0.0421.

Constraints and Clarifications

  • visitors is at least 1, and conversions is between 0 and visitors .
  • No column is NULL.
  • z_critical has exactly one row with confidence = 0.95 . Rows for other levels must not affect the result.
  • Use the formula as given, with no continuity correction.