WITHIN GROUP is part of the SQL-standard syntax for ordered-set and hypothetical-set aggregate functions such as PERCENTILE_CONT, PERCENTILE_DISC, MODE, and RANK. The clause lets you supply an ORDER BY expression list that the database must use to order the input rows prior to applying the aggregate’s mathematical logic. Without WITHIN GROUP, the function would have no deterministic way to locate, for example, the 90th percentile value or the first element in a distribution. Behavior:- Executes after the WHERE, GROUP BY, and HAVING phases but before the final SELECT projection is returned.- Works only in combination with an aggregate function that explicitly supports ordered sets. Trying to use it with SUM or COUNT raises an error.- Accepts multiple columns and ASC or DESC modifiers, mirroring the standard ORDER BY syntax.Caveats:- You cannot reference column aliases defined in the SELECT list.- NULLS FIRST | LAST support depends on the database; PostgreSQL supports it, MySQL does not (as of 8.0).- Because ordered-set aggregates often require sorting large intermediate sets, performance can degrade on big tables lacking proper indexes.
value_expression
(any data type) - the column or expression whose distribution you are analyzing.sort_expression
(any comparable data type) - determines row ordering before aggregation.[ASC
(DESC]) - keyword|||optional direction; default ASC.[NULLS FIRST
(LAST]) - keyword|||optional null-placement; vendor support varies.ORDER BY, GROUP BY, WINDOW FUNCTIONS, PERCENTILE_CONT, MODE, RANK
SQL:2003 (ordered-set aggregates)
The database raises a syntax error because the ORDER BY list is mandatory; the aggregate needs a defined ordering to operate.
Often yes, because the optimizer can leverage indexes and stream aggregates, but performance depends on data size and indexing.
Call PERCENTILE_CONT multiple times with different percentile arguments inside the same SELECT clause, each with its own WITHIN GROUP.
Your database version might not support the ordered-set function, or you are passing an unsupported data type.