Learn SQL
-- aggregations.stats_aggregates_stddev

Statistical Aggregates in SQL

Spread, not just center — STDDEV, VARIANCE, buckets

--concept

Beyond SUM/AVG/MIN/MAX: STDDEV/VARIANCE measure spread (salary dispersion), PERCENTILE_DISC picks actual-row percentiles vs CONT interpolation, CUME_DIST/PERCENT_RANK position relatively, NTILE buckets for deciles/quartiles. Mean lies on skew; spread + median + buckets tell truth. All skip NULLs like sibling aggregates.

--how it works
1

Center + spread

AVG with STDDEV/VARIANCE — mean without dispersion misleads.

2

Percentile pick

CONT interpolates median; DISC returns an actual row value.

3

Rank relatively

PERCENT_RANK vs CUME_DIST — position vs cumulative share.

4

Bucket for bands

NTILE deciles/quartiles for pay bands and stratified samples.

--visualization
meanspread
6766717685
Stage 1 / 1
CONT interpolates median; DISC returns an actual row; NTILE deciles band.
--example
query.sql
1SELECT STDDEV(salary) AS spread, AVG(salary) AS mean FROM employees;
Result
spreadmean
1768567667

Mean 67k with ±17k spread — two numbers beat one. Median/NTILE would further show whether the middle clusters or splits.

--variants

Mean ± spread

Two numbers beat one.

query.sql
1SELECT AVG(salary) AS mean, STDDEV(salary) AS spread FROM employees;
Result
meanspread
6766717685
1Mean centers; spread sizes the cloud.
2Same mean, wider spread = wider bands.
3Per-dept GROUP BY localizes dispersion.

Actual-row percentile + deciles

DISC picks a real salary; NTILE bands.

query.sql
1SELECT PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY salary) AS median_row FROM employees;
Result
median_row
73000
1DISC returns Farid’s 73k (a real row).
2CONT would interpolate between rows.
3NTILE(10) would decile the ladder.
--key facts
SpreadSTDDEV/VARIANCE beside mean
CONT vs DISCinterpolated vs actual-row percentile
NTILEdecile/quartile bucketing
--use cases
Comp analysis: mean/median/spread/deciles together
SLA p50/p95 with actual-row grounding
--under the hood
Ordered-set aggs sort once — GROUP BY variants sort per group; NULLs skipped like siblings.
Approximate percentiles (t-digest) trade exactness for streaming scale.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.