GROUPING is a scalar function defined in the SQL:1999 standard that works with advanced GROUP BY extensions such as ROLLUP, CUBE, and GROUPING SETS. When these operations produce subtotal or super-aggregate rows, columns not included in that specific grouping level are set to NULL. Because real data can also contain NULLs, GROUPING provides a reliable flag to distinguish system-generated NULLs from stored NULLs.The function evaluates each listed column or expression and returns an integer: 1 if the column was aggregated (not part of the grouping key) and 0 if it was present in the grouping key. GROUPING is deterministic, can appear in SELECT and ORDER BY clauses, and can be combined into a single bitmask via GROUPING_ID in some dialects.Caveats:- Only valid in the same SELECT list that contains a GROUP BY with ROLLUP, CUBE, or GROUPING SETS.- Returns 0 or 1; it is not an aggregate itself.- Cannot reference aliases defined in the same SELECT list.
column_or_expression
(any) - The column name or expression whose grouping status you want to checkGROUP BY, GROUPING SETS, ROLLUP, CUBE, GROUPING_ID, aggregate functions
SQL:1999
It flags whether each column in the SELECT list is aggregated (1) or part of the current grouping level (0) when you use ROLLUP, CUBE, or GROUPING SETS.
GROUPING evaluates one column at a time. GROUPING_ID combines multiple GROUPING results into a single bitmask, letting you rank grouping levels more easily.
Yes. Add `HAVING GROUPING(column) = 1` or similar logic to keep or exclude subtotal rows.
No. MySQL currently lacks native support for the GROUPING function, though you can emulate some behavior with conditional aggregation.