PostgreSQL - SUM() Function

Last Updated : 1 Aug, 2026

The SUM() function in PostgreSQL returns the total of all non-NULL values in a numeric column. It is commonly used to calculate totals for numeric data.

  • Calculate the total value of a numeric column.
  • Find the sum of values for specific groups using GROUP BY.
  • Generate summary reports from numeric data.

Syntax

SELECT SUM(column_name)
FROM table_name;
  • SUM(): Calculates the total of all non-NULL values in the specified numeric column.
  • column_name: The numeric column whose values are added.
  • table_name: The table from which the values are retrieved.

Examples

Firstly, create a Sales table and insert some records.

Screenshot-2026-07-27-113335
Sales Table

Example 1: Find the Total Sales Amount

Query:

SELECT SUM(Amount) AS TotalSales
FROM Sales;

Output:

Screenshot-2026-07-27-112217
  • The SUM() function returns the total sales amount from the Sales table.

Example 2: Find the Total Sales by Category

Query:

SELECT Category, SUM(Amount) AS TotalSales
FROM Sales
GROUP BY Category;

Output:


Screenshot-2026-07-27-112410
  • The GROUP BY clause groups the records by category and SUM() returns the total sales amount for each category.
Comment

Explore