PostgreSQL SUM Function
Summary: in this tutorial, you’ll learn how to use PostgreSQL SUM()
function to calculate the sum of a set of values.
Introduction to PostgreSQL SUM() function
The PostgreSQL SUM()
is an aggregate function that returns the sum of values in a set.
Here’s the basic syntax of the SUM()
function:
The SUM()
function ignores NULL
, meaning that it doesn’t consider the NULL
in calculation.
If you use the DISTINCT
option, the SUM()
function calculates the sum of only distinct values.
For example, without the DISTINCT
option, the SUM()
of 1, 1, and 2 will return 4. But the sum of distinct values of 1, 1, and 2 will return 3 (1 + 2) because the SUM()
function ignores one duplicate value (1).
The SUM()
of an empty set will return NULL
, not zero.
PostgreSQL SUM() function examples
Let’s take some examples of using the SUM()
function. We’ll use the payment
table in the sample database.
1) Using PostgreSQL SUM() function in SELECT statement example
The following example uses the SUM()
function to calculate the sum of all payments in the payment
table:
Output:
2) Using PostgreSQL SUM() function with an empty result set
The following statement uses the SUM()
function to calculate the total payment of the customer id 2000.
Output:
In this example, the SUM()
function returns a NULL
because the payment
the table has no row with the customer_id
2000.
3) Using the SUM() function with COALESCE() function example
If you want the SUM()
function to return zero instead of NULL
in case there is no matching row, you use the COALESCE()
function.
The COALESCE()
function returns the first non-null argument. In other words, it returns the second argument if the first argument is NULL
.
The following query illustrates how to use the SUM()
function with the COALESCE()
function:
Output:
4) Using PostgreSQL SUM() function with the GROUP BY clause example
To calculate the summary of every group, you use the GROUP BY
clause to group the rows in the table into groups and apply the SUM()
function to each group.
The following example uses the SUM()
function with the GROUP BY
clause to calculate the total amount paid by each customer:
Output:
The following query retrieves the top five customers who made the highest payments:
Output:
5) Using PostgreSQL SUM() function with a HAVING clause
To filter group sums based on a specific condition, you use the SUM()
function in the HAVING
clause.
The following example retrieves customers who have made payments exceeding 200:
Output:
6) Using PostgreSQL SUM() function with an expression
See the following rental
table from the sample database:
The following statement uses the SUM()
function to calculate the total rental days:
Output:
How it works.
- First, calculate the rental duration by subtracting the rental date from the return date.
- Second, apply the
SUM()
function to the expression.
The following example uses the SUM()
function to calculate the total duration by customers:
Output:
Summary
- Use the
SUM()
function to calculate the sum of values in a set. - Use the
DISTINCT
option in theSUM()
function to calculate the sum of distinct values. - Use the
SUM()
function with theGROUP BY
clause to calculate the sum for each group.