SQL STDDEV

The function that computes standard deviation — and the sample-vs-population choice that changes the number slightly.

What & Why

Postgres offers STDDEV_POP (treats your data as the entire population) and STDDEV_SAMP (treats it as a sample of a larger population). Plain STDDEV is an alias for STDDEV_SAMP.

See How It Works

BUSINESS QUESTION

Marketing wants spend variability by campaign channel.

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
SELECT
  channel,
  ROUND(STDDEV(spend), 2) AS spend_stddev
FROM marketing.campaigns
GROUP BY channel
ORDER BY channel;
RESULT — spend standard deviation
channelspend_stddev
emailNULL
google_ads3535.53
linkedinNULL

PostgreSQL STDDEV is the sample standard deviation.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario