Computing standard deviation, variance and correlation in SQL: population vs sample variants

Quick Overview

Asks how to compute variance, standard deviation and correlation with SQL aggregates and how the population and sample versions differ. Tests knowledge of built-in statistical functions, differences between database engines, NULL handling, and when each version is the right one to report.

Computing standard deviation, variance and correlation in SQL: population vs sample variants

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How would you compute statistical measures like standard deviation, variance, or correlation directly in SQL? Explain the difference between population and sample variants. ```hint Check your engine Look up which statistical aggregates your database ships, and what the unsuffixed names such as `STDDEV` and `VARIANCE` actually compute there. ``` ```hint Think about the rows Ask whether your rows are every unit you care about or a draw from something larger, and how that should change the estimate of spread. ``` ### What a Strong Answer Covers - The built-in aggregates for variance, standard deviation, covariance and correlation, and how to use them with GROUP BY or as window functions. - The difference between population and sample variants, including the formulas and when each is appropriate. - Engine differences in what bare `STDDEV` and `VARIANCE` mean, and how to compute the measures by hand where a function is missing. - How NULLs, single-row groups, integer arithmetic and numerical precision affect the results. ### Follow-up Questions - Your engine has no `CORR` function. How would you compute Pearson correlation from plain aggregates, and what numerical problem can a one-pass formula run into? - Why does `CORR` return the same value whether you treat the rows as a population or a sample? - How would you compute a 7-day rolling standard deviation per user in SQL? - Is the sample standard deviation an unbiased estimate of the population standard deviation?

Overview: Asks how to compute variance, standard deviation and correlation with SQL aggregates and how the population and sample versions differ. Tests knowledge of built-in statistical functions, differences between database engines, NULL handling, and when each version is the right one to report.

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

How would you compute statistical measures like standard deviation, variance, or correlation directly in SQL? Explain the difference between population and sample variants.

What a Strong Answer Covers Guidance

  • The built-in aggregates for variance, standard deviation, covariance and correlation, and how to use them with GROUP BY or as window functions.
  • The difference between population and sample variants, including the formulas and when each is appropriate.
  • Engine differences in what bare STDDEV and VARIANCE mean, and how to compute the measures by hand where a function is missing.
  • How NULLs, single-row groups, integer arithmetic and numerical precision affect the results.

Follow-up Questions Guidance

  • Your engine has no CORR function. How would you compute Pearson correlation from plain aggregates, and what numerical problem can a one-pass formula run into?
  • Why does CORR return the same value whether you treat the rows as a population or a sample?
  • How would you compute a 7-day rolling standard deviation per user in SQL?
  • Is the sample standard deviation an unbiased estimate of the population standard deviation?