SQL Standard Deviation

A single number that answers: how spread out are these values around their average?

What & Why

Standard deviation measures how far, on average, values sit from the mean. A small standard deviation means values cluster tightly around the average; a large one means they're scattered widely.

Two datasets can share the exact same average while looking completely different — standard deviation is what tells them apart.

See How It Works

BUSINESS QUESTION

Measure campaign-spend variability within each marketing 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,
  COUNT(*) AS campaigns,
  ROUND(AVG(spend), 2) AS avg_spend,
  ROUND(STDDEV_SAMP(spend), 2) AS sample_stddev_spend
FROM marketing.campaigns
GROUP BY channel
ORDER BY sample_stddev_spend DESC NULLS LAST, channel;
RESULT — sample spend deviation
channelcampaignsavg_spendsample_stddev_spend
google_ads257500.003535.53
email145000.00NULL
linkedin150000.00NULL

A sample standard deviation needs at least two rows, so only google_ads has a value.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario