Statistical Aggregates in SQL
Spread, not just center — STDDEV, VARIANCE, buckets
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.
Center + spread
AVG with STDDEV/VARIANCE — mean without dispersion misleads.
Percentile pick
CONT interpolates median; DISC returns an actual row value.
Rank relatively
PERCENT_RANK vs CUME_DIST — position vs cumulative share.
Bucket for bands
NTILE deciles/quartiles for pay bands and stratified samples.
1SELECT STDDEV(salary) AS spread, AVG(salary) AS mean FROM employees;
| spread | mean |
|---|---|
| 17685 | 67667 |
Mean 67k with ±17k spread — two numbers beat one. Median/NTILE would further show whether the middle clusters or splits.
Mean ± spread
Two numbers beat one.
1SELECT AVG(salary) AS mean, STDDEV(salary) AS spread FROM employees;
| mean | spread |
|---|---|
| 67667 | 17685 |
Actual-row percentile + deciles
DISC picks a real salary; NTILE bands.
1SELECT PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY salary) AS median_row FROM employees;
| median_row |
|---|
| 73000 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.