Skip to content

GROUP BY and ORDER BY in BigQuery

GROUP BY

The GROUP BY clause groups together rows in a table that share common values for certain columns. For a group of rows in the source table with non-distinct values, the GROUP BY clause aggregates them into a single combined row.

For example, I want to find out each product’s revenue.

SELECT NAME, SUM(Price * Sold) AS Revenue
FROM `instruction-415216.examples.ivys_shop` 
GROUP BY Name

It returned 20 results (because we have 20 products) and here we only show a few results.

RowNAMERevenue
1Kiwi1563.7700000000002
2Apple120.56
3Mango228.55
4Peach236.91
5Banana582.07
6Grapes1411.47

You can group by multiple conditions or calculate more meaningful metrics, too.

For example, besides the total revenue, I want to the average price and average sold quantity.

SELECT NAME,
       SUM(Price * Sold) AS Product_Revenue,
       AVG(Price) AS  avg_price,
       AVG(Sold) AS  avg_sold_quantity,
FROM `instruction-415216.examples.ivys_shop` 
GROUP BY Name
RowNAMEProduct_Revenueavg_priceavg_sold_quantity
1Kiwi1563.77000000000025.373749999999999435.375
2Apple120.565.4822.0
3Mango228.556.563333333333333511.0
4Peach236.912.542536.75
5Banana582.075.171428571428571714.1428571428571

ORDER BY

The ORDER BY clause specifies a column or expression as the sort criterion for the result set. If an ORDER BY clause is not present, the order of the results of a query is not defined. 

ASC | DESC: Sort the results in ascending or descending order of expression values. ASC is the default value.

In the first example, the revenue column is not sorted. I want to see the top 5 highest revenue at first glance. We can use ORDER BY in DESC way.

SELECT NAME, SUM(Price * Sold) AS Revenue
FROM `instruction-415216.examples.ivys_shop` 
GROUP BY Name
ORDER BY Revenue DESC
LIMIT 5
RowNAMERevenue
1Potato1724.5
2Kiwi1563.7700000000002
3Grapes1411.47
4Carrot1329.54
5Orange998.83

In the second example, I want to sort the avg_price and avg_sold_quantity in ascending way.

SELECT NAME,
       SUM(Price * Sold) AS Product_Revenue,
       AVG(Price) AS  avg_price,
       AVG(Sold) AS  avg_sold_quantity,
FROM `instruction-415216.examples.ivys_shop` 
GROUP BY Name
ORDER BY avg_price, avg_sold_quantity ASC
LIMIT 5

When sorting by multiple columns in SQL, the order in which you specify the columns determines the precedence of the sorting. The columns are sorted in the order they are listed in the ORDER BY clause. The column listed first has the highest precedence, followed by the second column, and so on.

RowNAMEProduct_Revenueavg_priceavg_sold_quantity
1Peach236.912.542536.75
2Spinach536.393.018333333333333129.333333333333336
3Bell Pepper927.500000000000114.279999999999999441.166666666666664
4Lettuce334.224.287518.75
5Watermelon50.794.57666666666666624.66666666666666