SUM function in BigQuery

The SUM function in BigQuery is an aggregate function that calculates the total sum of a numeric column.

SQL Syntax

SELECT column_name, SUM(column_to_sum) AS total_sum
FROM `project_name.dataset_name.table_name`
WHERE condition
GROUP BY column_name
HAVING SUM(column_to_sum) > some_value;  

Example: Let’s have the following employees table in BigQuery.

By executing the following query, we can see all the rows from the table.

SQL

SELECT * FROM `ashishcoder.Coding_Dataset.employees`;  

The output of the above code is shown below:

Sum function in BigQuery

To find the total salary amount:

SQL

SELECT SUM(Salary) AS total_salary
FROM `ashishcoder.Coding_Dataset.employees`; 

The output of the above code is shown below:

Sum function in BigQuery

Use GROUP BY Clause

To find the total Salary amount for each department:

SQL

SELECT Department, SUM(Salary) AS total_salary 
FROM `ashishcoder.Coding_Dataset.employees`
group by Department; 

The output of the above code is shown below:

Sum function in BigQuery