PostgreSQL - SUM() Function - GeeksforGeeks

PostgreSQL - SUM() Function

Last Updated : 7 Nov, 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-10-05-123022

Example 1: Find the Total Sales amount

Query:

SELECT SUM(amount) AS total_sales
FROM sales;

Output:

Screenshot-2026-10-05-122924
  • 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 total_sales
FROM sales
GROUP BY category;

Output:

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

Explore