PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Functions / PostgreSQL SUM Function

PostgreSQL SUM Function

Summary: this tutorial shows you how to use PostgreSQL SUM function to calculate the sum of values or distinct values.

Introduction to PostgreSQL SUM function

The PostgreSQL SUM function returns the sum of values or distinct values. The syntax of the SUM function is as follows:

1
SUM(DISTINCT column)

There are some important points you should remember when using the SUM function:

  • The SUM function ignores NULL values. It means the NULL values are not considered in calculation of the SUM function.
  • If you use DISTINCT operator, the SUM function only calculates the values of distinct values. For example, without the DISTINCT operator, the SUM function returns 12 when calculating the sum of 1, 1, 8, and 2. However, with DISTINCT operator, the SUM function will return 11  (1 + 8 + 2).
  • If you use the SUM function in a SELECT clause, it returns a NULL value instead of zero (0) in case the SELECT statement returns no rows.

PostgreSQL SUM function examples

We will use the payment table in the sample database to demonstrate the functionality of the SUM function.

payment table

Let’s calculate the sum of payment paid by customer id 2000.

1
2
3
4
5
6
SELECT
SUM (amount) AS total
FROM
payment
WHERE
customer_id = 2000;

SUM return NULL

Because the condition in the WHERE clause filters all rows, the SUM function returns a NULL value as mentioned above. No row exists in the payment table with customer_id 2000.

Sometimes, you want the SUM function returns zero instead of a NULL value in case there is no matching row found by the SELECT statement. In this case, you use the COALESCE function that returns the first argument as a default value if the second argument is NULL.

The following query illustrates the SUM function with the COALESCE function:

1
2
3
4
5
6
SELECT
COALESCE(0,SUM(amount)) AS total
FROM
payment
WHERE
customer_id = 2000;

SUM with COALESCE

PostgreSQL SUM function with GROUP BY clause

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 for each group.

For example, to calculate the total amount paid by each customer, you use the SUM function with the GROUP BY clause as follows:

1
2
3
4
5
6
7
SELECT
customer_id,
SUM (amount) AS total
FROM
payment
GROUP BY
customer_id;

SUM with GROUP BY

The following query returns top 5 customers who paid the most:

1
2
3
4
5
6
7
8
9
SELECT
customer_id,
SUM (amount) AS total
FROM
payment
GROUP BY
customer_id
ORDER BY total DESC
LIMIT 5;

SUM top 5 customers

The following diagram illustrates how PostgreSQL performs the query:

postgresql sum with group by

PostgreSQL SUM function with HAVING clause

To filter the sums of groups based on a specific condition, you use the SUM function in the HAVING clause. For example, the following query returns only customers who paid more than $200:

1
2
3
4
5
6
7
8
9
SELECT
customer_id,
SUM (amount) AS total
FROM
payment
GROUP BY
customer_id
HAVING SUM(amount) > 200
ORDER BY total DESC

SUM with HAVING

In this tutorial, you have learned how to use the PostgreSQL SUM function to calculate the total of values or distinct values.

Related Tutorials

  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL COUNT Function
Previous Tutorial: PostgreSQL MIN Function
Next Tutorial: PostgreSQL CASE

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Aggregate Functions

  • PostgreSQL COUNT Function
  • PostgreSQL AVG Function
  • PostgreSQL MAX Function
  • PostgreSQL MIN Function
  • PostgreSQL SUM Function

PostgreSQL Conditional Expressions

  • PostgreSQL CASE
  • PostgreSQL NULLIF
  • PostgreSQL COALESCE

PostgreSQL String functions

  • PostgreSQL CONCAT Function
  • PostgreSQL TRIM Function
  • PostgreSQL LENGTH Function
  • PostgreSQL REPLACE
  • PostgreSQL Substring
  • PostgreSQL Letter Case Functions

PostgreSQL Operators

  • PostgreSQL CAST

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.