SQL Practice Logo

SQLPractice Online

Statistical & Advanced Aggregate Functions: Mistakes

Module: Aggregate Functions & Grouping

SELECT department, PERCENTILE_CONT(0.5) FROM employees GROUP BY department;

SELECT department, PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) FROM employees GROUP BY department;

Percentile functions require WITHIN GROUP (ORDER BY column) to specify ordering.

Always include WITHIN GROUP (ORDER BY col) with percentile functions

High

Missing WITHIN GROUP clause

SELECT AVG(salary) AS avg, STDDEV(salary) AS std FROM employees WHERE department = 'Sales';

SELECT department, AVG(salary) AS avg, STDDEV(salary) AS std FROM employees GROUP BY department HAVING COUNT(*) >= 30;

Statistical functions need sufficient sample size. HAVING ensures minimum group size.

Ensure adequate sample size for statistical validity (typically n≥30)

Medium

Small sample size for statistical validity