View a markdown version of this page

GROUP BY clause - Amazon Athena

GROUP BY clause

The GROUP BY clause puts the rows that have the same values of the grouping expressions into one group, and the query returns one row for each group. Aggregate functions such as count and sum then compute a value over the rows of each group. A grouping expression can be a column, an expression, an alias of the select list, or the position of a select list item, where 1 is the first.

GROUP BY ALL groups by every item of the select list that does not contain an aggregate function, so you do not have to repeat them.

GROUPING SETS computes several groupings in one query and returns their rows together. ROLLUP (a, b) is short for GROUPING SETS ((a, b), (a), ()), which adds subtotal and grand total rows. CUBE (a, b) is short for GROUPING SETS ((a, b), (a), (b), ()), which covers every combination. In a row of a grouping that leaves out a column, that column is NULL.

To tell those NULL values from NULL data, use grouping(column) in the select list. It returns 1 when the column is not part of the row's grouping and 0 when it is. With several columns, grouping returns a bit mask with one bit for each column, with the last column in the lowest bit.

Syntax

GROUP BY expression [, ...] GROUP BY ALL GROUP BY GROUPING SETS ( ( [ expression [, ...] ] ) [, ...] ) GROUP BY ROLLUP ( expression [, ...] ) GROUP BY CUBE ( expression [, ...] )

Examples

For a description of the sample tables that the examples on this page use, see SELECT statement in DuckDB.

The following SQL query counts the orders and totals the amounts in each region. sum skips the NULL amount.

SELECT region, count(*) AS orders, sum(amount) AS revenue FROM sales_db.sales_parquet GROUP BY region ORDER BY region

The query returns the following results.

region orders revenue

east

3

162.75

north

1

310.00

west

2

93.75

The following SQL query uses GROUP BY ALL, which groups by region and order_date, the two select list items that are not aggregates.

SELECT region, order_date, count(*) AS orders FROM sales_db.sales_parquet WHERE region = 'west' OR order_date = DATE '2026-10-01' GROUP BY ALL ORDER BY region, order_date

The query returns the following results.

region order_date orders

east

2026-10-01

1

west

2026-10-01

1

west

2026-10-03

1

The following SQL query groups by region_code, an alias defined in the select list.

SELECT upper(region) AS region_code, count(*) AS orders FROM sales_db.sales_parquet GROUP BY region_code ORDER BY region_code

The query returns the following results.

region_code orders

EAST

3

NORTH

1

WEST

2

The following SQL query counts orders by region and, separately, by date, for the regions other than east.

SELECT region, order_date, count(*) AS orders FROM sales_db.sales_parquet WHERE region <> 'east' GROUP BY GROUPING SETS ((region), (order_date)) ORDER BY region NULLS LAST, order_date NULLS LAST

The query returns the following results.

region order_date orders

north

NULL

1

west

NULL

2

NULL

2026-10-01

1

NULL

2026-10-03

2

The following SQL query adds a grand total row with ROLLUP, and uses grouping(region) to mark it.

SELECT region, grouping(region) AS is_total, sum(amount) AS revenue FROM sales_db.sales_parquet GROUP BY ROLLUP (region) ORDER BY is_total, region

The query returns the following results.

region is_total revenue

east

0

162.75

north

0

310.00

west

0

93.75

NULL

1

566.50

The following SQL query uses CUBE to count the orders in the west region by every combination of region and date.

SELECT region, order_date, count(*) AS orders FROM sales_db.sales_parquet WHERE region = 'west' GROUP BY CUBE (region, order_date) ORDER BY region NULLS LAST, order_date NULLS LAST

The query returns the following results.

region order_date orders

west

2026-10-01

1

west

2026-10-03

1

west

NULL

2

NULL

2026-10-01

1

NULL

2026-10-03

1

NULL

NULL

2