Statistical & Advanced Aggregate Functions: Functions
Module: Aggregate Functions & Grouping
-- Standard deviation and variance
SELECT
department,
AVG(salary) AS avg_salary,
STDDEV(salary) AS std_dev,
VARIANCE(salary) AS variance
FROM employees
GROUP BY department;
-- Percentiles (PostgreSQL)
SELECT
department,
PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY salary) AS p25,
PERCENTILE_CONT(0.50) WITHIN GROUP (ORDER BY salary) AS median,
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY salary) AS p75
FROM employees
GROUP BY department;
-- Combined statistical analysis
SELECT
department,
COUNT(*) AS employee_count,
AVG(salary) AS avg_salary,
STDDEV(salary) AS std_dev,
MIN(salary) AS min_salary,
MAX(salary) AS max_salary
FROM employees
GROUP BY department;
STDDEV(column) calculates standard deviation
VARIANCE(column) calculates variance
PERCENTILE_CONT for interpolated percentiles
PERCENTILE_DISC for actual data values
WITHIN GROUP (ORDER BY col) required for percentiles
NULL values ignored in calculations
Full support: STDDEV, VARIANCE, PERCENTILE_CONT, PERCENTILE_DISC
Limited: STDDEV, VARIANCE (no percentile functions)
STDEV, VAR, PERCENTILE_CONT, PERCENTILE_DISC
Core references in this topic include WHERE, =, <, >, <=, >=. Learn what each one does, when to use it, and the execution or engine rules that matter.
WHERE
Filters rows before projection and sorting. It decides which rows continue through the query pipeline.
SELECT ... FROM table WHERE condition;
Most performance issues start with a weak WHERE clause or a missing supporting index.