Statistical & Advanced Aggregate Functions: Examples
Module: Aggregate Functions & Grouping
Salary Variance Analysis
intermediate
HR analyzes salary spread to identify pay equity issues
SELECT
department,
COUNT(*) AS employee_count,
AVG(salary) AS avg_salary,
STDDEV(salary) AS salary_std_dev,
VARIANCE(salary) AS salary_variance
FROM employees
GROUP BY department
ORDER BY salary_std_dev DESC;
department | employee_count | avg_salary | salary_std_dev | salary_variance
Engineering | 45 | 95000 | 18500 | 342250000
Sales | 30 | 75000 | 8200 | 67240000
High standard deviation indicates wide salary spread, potential pay equity issues. Engineering has more variance than Sales.
PostgreSQL
Percentile Analysis
advanced
Calculate salary quartiles for compensation benchmarking
SELECT
department,
PERCENTILE_CONT(0.25) WITHIN GROUP (ORDER BY salary) AS p25_salary,
PERCENTILE_CONT(0.50) WITHIN GROUP (ORDER BY salary) AS median_salary,
PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY salary) AS p75_salary,
AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
department | p25_salary | median_salary | p75_salary | avg_salary
Engineering | 82000 | 95000 | 110000 | 95000
Sales | 68000 | 75000 | 82000 | 75000
Percentiles show salary distribution. Median less affected by outliers than average. 25th-75th percentile shows middle 50% range.
PostgreSQL
Quality Control with Standard Deviation
intermediate
Manufacturing analyzes product weight variance for quality control
SELECT
product_line,
COUNT(*) AS units_produced,
AVG(weight_grams) AS avg_weight,