Overview
SUM() calculates the sum of values from stored records. SUM() doesn’t consider NULL in the calculation, and it returns NULL instead of zero if the executed statement returns no rows.
The input and return types we support can be seen in the table below.
| Input type | Return type | 
|---|---|
| INT | LONG | 
| LONG | LONG | 
| FLOAT | DOUBLE | 
| DOUBLE | DOUBLE | 
| INTERVAL | INTERVAL | 
If the input type is 32-bit, then the result will be 64-bit.
Examples
We have two sample tables here: customer table#Case 1: SUM() in SELECT statement
The following example uses the SUM() function to calculate the total rent price of all rental_id:
total_price:
#Case 2: SUM() with a NULL result
The following example uses the SUM() function to calculate the total rent price of the customer_id = 11118.
customer_id = 11118, the SUM() function returns a NULL.
#Case 3: SUM() with GROUP BY clause
You can use the GROUP BY clause to group the records in the table and apply the SUM() function to each group afterward.
The following example uses the SUM() function and the GROUP BY clause to calculate the total price paid by each customer:
total_price from a group of customer_id as shown below:
#Case 4: SUM() with HAVING clause
You can use the SUM() function with the HAVING clause to filter out the sum of groups based on a specific condition:
#Case 5: SUM() with multiple expression
The example uses the following:
- 
SUM()function to calculate total rental days. - 
JOINclause to combine the rental table with the customer table. - 
GROUP BYgroup a result-set based on the customers’ names.