Practical SQL

Using GROUP BY ALL in BigQuery

Constantin LunguUpdated 1 min read

Photo by Monika Borys on Unsplash

Featured in other database systems, the GROUP BY ALL has been announced in preview for BigQuery as well.

This will allow us to not enumerate all the non-aggregated columns when performing aggregates.

It's definitely better than GROUP BY 1,2,3 which would fail once we'd change the list of columns we'd like to group by. Overall, I find it a useful shorthand when exploring or debugging.

Here's an example of how it looks.

SELECT country, sell_date, SUM(sales) AS total_sales

FROM input_data

-- new
GROUP BY ALL

-- instead of
-- GROUP BY country, sell_date

BigQuery console Results tab for the GROUP BY ALL query, with columns country, sell_date and total_sales and one row per country and date: UK 150 and 195, US 200 and 260 for 2021-01-01 and 2021-01-02.