EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial Aggregate Functions in PostgreSQL
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

Aggregate Functions in PostgreSQL

By Priya PedamkarPriya Pedamkar

Aggregate Functions in PostgreSQL

Introduction to Aggregate Function in PostgreSQL

PostgreSQL aggregate functions are used to compute the set of input values in one result. It produced a single result for an entire group of tables. PostgreSQL aggregate functions used to produce a summarized set of results. They return results based on a group of rows set. Aggregate functions will treat all rows of a table as a group by default. The statement like group by clause of the select statement is used to divide all rows into smaller groups or chunks. Aggregate functions support for aggregating multiple rows in a single dataset. We can see a list of aggregate functions using the \df command.

Various Aggregate Functions in PostgreSQL

Below is the list of aggregate functions in PostgreSQL is as follows.

  • COUNT
  • SUM
  • MIN
  • MAX
  • AVG

Below is a detailed description of aggregate functions. Also below is the syntax of aggregate functions. We have used the customer table as an example.

Customer table:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

select * from Customer;

aggregate functions

select version();

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,584 ratings)

aggregate functions

Syntax:

  • Aggregate function using the expression:

aggregate_name (expression [... , ] [order_by_clause] )

  • Aggregate function using ALL –

aggregate_name (ALL expression [,...] [ order_by_clause] )

  • Aggregate function using DISTINCT clause:

aggregate_name (DISTINCT expression [,...] [ order_by_clause] )

  • Aggregate function using the asterisk(*):

aggregate_name (*)

Below is the parameter description are as follows.

  • Aggregate name: Name of the aggregate function.
  • Expression: This is the value or value of a column in an aggregate function, which does not contain any aggregate expression.
  • Order by the clause: It is optional used to set to arrange results in a set of orders.

1. Count Aggregate Function

  • The count aggregate function in PostgreSQL returns the count of rows from a selected number of columns.

Syntax:

COUNT (* | DISTINCT ALL | Column_name)

Below is the parameter description of syntax are as follows.

  • Column name: Name of the column that we have used in the query to count the records’ values.
  • Asterisk (*): The Asterisk (*) indicates all the rows, it will return the count of rows of the column which have used in a query.
  • DISTINCT: This clause is used to find unique value from the table; also, this parameter is optional.
  • ALL: This is the default clause of the count aggregate function also; this is optional.

Example:

1. If we want to get a total count of rows from the customer table.

testing=# select COUNT (cust_balance) from customer;

select COUNT

2. If we want to get a count of distinct rows for the customer balance column from the customer table.

testing=# select COUNT (distinct(cust_balance)) from customer;

distinct cust_balance

3. If we want to get the count of rows for the cust_id column from the customer table.

testing=# select count(cust_id) from customer;

cust_id

select count (*) from a customer;

aggregate functions

select count (1) from a customer;

aggregate functions

select count (cust_name) from customer;

aggregate functions

select count (distinct(cust_name)) from customer;

aggregate functions

2. SUM Aggregate Function

The sum aggregate function in PostgreSQL returns the sum of values from a selected number of columns. It will select a total of no numeric function and return a sum of all records.

Syntax:

SUM (* | DISTINCT ALL | Column_name)

Below is the parameter description of syntax are as follows.

  • Column name: Name of the column that we have used in the query to sum the records’ values.
  • Asterisk (*): The Asterisk (*) indicates all the rows, it will return the sum of columns that have used in a query.
  • DISTINCT: This clause is used to find unique value from the table; also, this parameter is optional.
  • ALL: This is a default clause of the SUM aggregate function; also, this is optional.

Example:

1. If we want to calculate the total sum of customer balance from the customer table.

testing=# select SUM (cust_balance) from customer;

select SUM

2. If we want to calculate the distinct sum of customer balance from the customer table.

testing=# select SUM(distinct(cust_balance)) from customer;

calculate distinct sum

3. MIN Aggregate Function

Min function returns the result of the smallest value of all selected values of the column. It will select the lowest value from a selected column.

Syntax:

MIN (* | [DISTINCT] ALL | Column_name)

Below is the parameter description of syntax are as follows.

  • Column name: Name of the column that we have used in the query.
  • Asterisk (*): The Asterisk (*) indicates all the rows, it will return all rows MIN value of tables.
  • DISTINCT: This clause is used to find the unique smallest value from the table.
  • ALL: This is the default clause of the MIN aggregate function also; this is optional.

Example:

1. If we want to calculate the minimum value of customer balance from the customer table.

testing=# select MIN (cust_balance) from customer;

calculate minimum value

2. If we want to calculate the distinct minimum value of customer balance from the customer table.

testing=# select MIN(distinct(cust_balance)) from customer;

distinct minimum value

select MIN(ALL(cust_balance)) from customer;

aggregate functions

4. MAX Aggregate Function

MAX function returns the result of the largest value of all selected values of the column. It will select the largest value from the selected column.

Syntax:

MAX (* | [DISTINCT] ALL | Column_name)

Below is the parameter description of syntax are as follows.

  • Column name: Name of column.
  • Asterisk (*): The Asterisk (*) indicates all the rows
  • DISTINCT: This clause is used to find the unique largest value from the table.
  • ALL: This is a default clause of the MAX aggregate function also; this is optional.

Example:

1. If we want to calculate the maximum customer balance from the customer table.

testing=# select MAX (cust_balance) from customer;

select MAX

2. If we want to calculate distinct maximum customer balance from the customer table.

testing=# select MAX(distinct(cust_balance)) from customer;

select MAX(distinct(cust_balance)

select MAX(ALL(cust_balance)) from customer;

select MAX(ALL(cust_balance)

5. AVG Aggregate Function

AVG function returns the average of all selected values of the column.

Syntax:

AVG (* | [DISTINCT] ALL | Column_name)

Below is the parameter description of syntax are as follows.

  • Column name: Name of the column that we have used to calculate the average.
  • Asterisk (*): The Asterisk (*) indicates all the rows
  • DISTINCT: This clause is used to find a unique average value from the table.
  • ALL: This is a default clause of the AVERAGE aggregate function also; this is optional.

Example:

1. If we want to calculate the maximum customer balance from the customer table.

testing=# select AVG (cust_balance) from customer;

maximum customer balance

2. If we want to calculate distinct maximum customer balance from the customer table.

testing=# select AVG(distinct(cust_balance)) from customer;

maximum customer balance

select AVG(ALL(cust_balance)) from customer;

maximum customer balance

Conclusion

PostgreSQL aggregate function is handy to find the result of tables. Mainly COUNT, MAX, MIN, AVG and SUM functions used in PostgreSQL. The aggregate function will support the aggregate no of columns in a table. The aggregate function will produce a single result of the entire group of tables.

Recommended Articles

This is a guide to Aggregate Functions in PostgreSQL. Here we discuss the basic concept, various aggregate functions in PostgreSQL and parameters along with respective examples. You can also go through our other related articles to learn more–

  1. SQL HAVING Clause
  2. AND in SQL
  3. MySQL Aggregate Function
  4. MySQL BETWEEN
  5. PostgreSQL GROUP BY | Examples | How to Work?
Popular Course in this category
SQL Training Program (7 Courses, 8+ Projects)
  7 Online Courses |  8 Hands-on Projects |  73+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more