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

Last updated 13 July 2026

  • SQL has built-in aggregates: VAR_POP/VAR_SAMP, STDDEV_POP/STDDEV_SAMP, CORR(y, x), and covariance functions.

  • Population variants divide by N (rows are the whole population). Sample variants divide by N-1 (Bessel's correction removes bias when estimating from a sample).

  • Bare STDDEV/VARIANCE differ by engine, so name the variant explicitly. NULLs are ignored, which shrinks N.

  • https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/statistical_aggregate_functions
  • https://docs.snowflake.com/pt/sql-reference/functions/corr
  • https://mariadb.com/docs/server/reference/sql-functions/aggregate-functions/var_samp
  • https://infocenter.sybase.com/help/topic/com.sybase.help.sqlanywhere.12.0.1/dbusage/ug-olap-s-51258143.html
  • https://www.dbvis.com/thetable/a-guide-to-the-sql-standard-deviation-functions/
  • https://campus.datacamp.com/courses/time-series-analysis-in-sql-server/aggregating-time-series-data?ex=5
All Advanced SQL interview questions