In this PostgreSQL tutorial, I will show you how to compute the total sum of columns in PostgreSQL. You will also understand the function SUM(), which computes the sum of all the values in a column. I will also explain the syntax of the SUM() function and how to use it.
Finally, you will implement the SUM() function with clauses like HAVING and GROUP with practical examples.
PostgreSQL Sum
PostgreSQL provides an aggregate function called SUM(), which returns the total sum of the column or the sum of the values within the column. This function is very helpful in decision-making, and businesses usually use it today.
Let me give you an example. Suppose a business wants to know its total sales. For that, it will use the SUM() to add up the daily sales. Based on the total sales, it can be decided whether the marketing strategy is effective or not.
Or maybe a business should invest in more stocks. This is one aspect of using the SUM(), but there can be other aspects.
Syntax
The syntax for the SUM() function in PostgreSQL is given below.
SUM(column_name)
- SUM(): It is the function itself that computes the total sum of columns.
- column_name: The column on which the SUM() function is applied, and the total sum of that column is returned.
First, I will show you a simple example of using the SUM() function and then proceed to a complex example, such as using the SUM() function on a table with different PostgreSQL clauses.
Case 1: Using SELECT
Let’s say you have a sales table with columns id, product_name, sale_date, and sale_amount and have to compute the total sales. The table is shown below.

Use the SUM() function on the column sale_amount as shown in the query below.
SELECT SUM(sale_amount) as total_sum FROM sales;
After executing the above query, I got the expected output as shown in the screenshot below.

The above query returns the total sum of column sale_amount as 8660.70 dollars using the SUM() function in the SELECT statement.
You have another task to compute total sales on a specific date, 2025-04-22, for that filter that day, and compute the total sales using the SUM() function.
Use the below query.
SELECT SUM(sale_amount) FROM sales
WHERE sale_date = '2025-04-22';
After executing the above query, I got the expected output as shown in the screenshot below.

The output shows that the total sales on 2025-04-22 were 1420.40 dollars, which is how the total sum of the filtered data from the column is computed.
Approach 2: With GROUP BY
Suppose you own an online store and want to find the total sales per product. Use the GROUP clause to see the total sales for each product.
Use the below query to compute the total sales per product.
SELECT product_name, SUM(sale_amount) as total_sales
FROM sales
GROUP BY product_name;
After executing the above query, I got the expected output as shown in the screenshot below.

The above output shows the total sales for each product, such as the Charger, which sold for 20.15 dollars, and the Desktop, which sold for 1100.00 dollars.
The above query first groups the products by their unique names using the GROUP BY product_name function. Then, for each group, the SUM() function is applied separately to compute the total sum of that group, as you can see in the above output.
To know about the GROUP BY clause, refer to this tutorial: Group By in PostgreSQL
Approach 3: With a having clause
The HAVING clause works like the WHERE clause in PostgreSQL, but unlike the WHERE clause, it works on aggregated data or results.
After computing the total sales on each product in the previous example, the store owner wants you to find the product with total sales of more than 1000 dollars.
So use the below query.
SELECT product_name, SUM(sale_amount) as total_sales
FROM sales
GROUP BY product_name
HAVING SUM(sale_amount) > 1000;
After executing the above query, I got the expected output as shown in the screenshot below.

The above query filters the total sales of products exceeding 1000 dollars using the SUM() function with the HAVING clause. If you remove the HAVING clause, you get the same result as in the previous example.
In this example, only the HAVING clause is used to filter products that have sold more than $1,000. There are four products: Desktop, Camera, Mobile Phone, and Laptop.
Conclusion
In this PostgreSQL tutorial, you learned how to use the SUM() function in PostgreSQL to find the total sum of columns. Additionally, you learned about using the SUM() function with clauses such as HAVING and GROUP.
You may also read:
I am Bijay having more than 15 years of experience in the Software Industry. During this time, I have worked on MariaDB and used it in a lot of projects. Most of our readers are from the United States, Canada, United Kingdom, Australia, New Zealand, etc.
Want to learn MariaDB? Check out all the articles and tutorials that I wrote on MariaDB. Also, I am a Microsoft MVP.