Compute sample variance, standard deviation and correlation of ad spend and revenue in SQL
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Return a single row with the sample standard deviation and sample variance of `ad_spend` and `revenue`, plus their Pearson correlation.
```hint Which rows count
Aggregate functions skip NULLs one column at a time. Think about what that does when every statistic has to be computed over the same set of rows.
```
### Input Table(s)
`campaign_metrics`
| Column | Type | Meaning |
|---|---|---|
| metric_id | INT | One observation. Primary key. |
| campaign_name | VARCHAR(100) | Campaign label. Not used in the calculation. |
| ad_spend | DECIMAL(10, 2) | Ad spend for the observation. Non-negative, may be NULL. |
| revenue | DECIMAL(10, 2) | Revenue for the observation. Non-negative, may be NULL. |
### Output Contract
Exactly one row with these columns, in this order:
| Column | Definition |
|---|---|
| spend_stddev | Sample standard deviation of `ad_spend`, rounded to 2 decimals |
| spend_variance | Sample variance of `ad_spend`, rounded to 2 decimals |
| revenue_stddev | Sample standard deviation of `revenue`, rounded to 2 decimals |
| revenue_variance | Sample variance of `revenue`, rounded to 2 decimals |
| corr_spend_revenue | Pearson correlation of `ad_spend` and `revenue`, rounded to 4 decimals |
Sample statistics divide by $n - 1$. The correlation is the covariance divided by the product of the two standard deviations, and it is NULL when that denominator is 0. Round only the final values.
### Example
`campaign_metrics`
| metric_id | campaign_name | ad_spend | revenue |
|---|---|---|---|
| 1 | a | 100 | 400 |
| 2 | b | 200 | 200 |
| 3 | c | NULL | 900 |
| 4 | d | 300 | NULL |
| 5 | e | 250 | 300 |
Expected result:
| spend_stddev | spend_variance | revenue_stddev | revenue_variance | corr_spend_revenue |
|---|---|---|---|---|
| 76.38 | 5833.33 | 100.00 | 10000.00 | -0.6547 |
Rows 3 and 4 are excluded because one of the two columns is NULL. All five statistics are computed over rows 1, 2 and 5 only: spend values 100, 200 and 250, revenue values 400, 200 and 300.
### Constraints and Clarifications
- Use only rows where both `ad_spend` and `revenue` are non-NULL, for every column of the output, including the per-column standard deviations and variances.
- `metric_id` is unique. Each row is a separate observation even if `campaign_name` repeats.
- The table has at most 10,000 rows.
- If fewer than two rows qualify, the sample statistics are undefined and every column is NULL. The query still returns one row.
- If either column is constant over the qualifying rows, its standard deviation is 0 and `corr_spend_revenue` is NULL.
Overview: Write a SQL query that returns the sample standard deviation and variance of ad spend and revenue, plus the Pearson correlation between them, in a single row. It tests sample versus population aggregates, consistent NULL filtering across columns, and guarding a correlation against a zero denominator.