PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • String Functions
    • Aggregate Functions
Home / PostgreSQL Functions / An Overview of PostgreSQL Aggregate Functions

An Overview of PostgreSQL Aggregate Functions

Summary: in this tutorial, you will learn how to use the PostgreSQL aggregate functions such as AVG(), COUNT(), MIN(), MAX(), and SUM().

Introduction to PostgreSQL aggregate functions

Aggregate functions perform a calculation on a set of rows and return a single row. PostgreSQL provides all standard SQL’s aggregate functions as follows:

  • AVG() – return the average value.
  • COUNT() – return the number of values.
  • MAX() – return the maximum value.
  • MIN() – return the minimum value.
  • SUM() – return the sum of all or distinct values.

We often use the aggregate functions with the GROUP BY clause in the SELECT statement. In these cases, the GROUP BY clause divides the result set into groups of rows and the aggregate functions perform a calculation on each group e.g., maximum, minimum, average, etc.

You can use aggregate functions as expressions only in the following clauses:

  •  SELECT clause.
  •  HAVING clause.

PostgreSQL aggregate functions examples

Let’s use the film table in the sample database for the demonstration.

AVG() function examples

The following statement uses the AVG() function to calculate the average replacement cost of all films:

1
2
3
4
SELECT
ROUND( AVG( replacement_cost ), 2 ) avg_replacement_cost
FROM
film;

The following is the result:

PostgreSQL aggregate functions - AVG example

Noted that the ROUND() function was used to round the result to 2 decimal places.

To calculate the average replacement cost of the Drama films whose category id is 7, you use the following statement:

1
2
3
4
5
6
7
8
SELECT
ROUND( AVG( replacement_cost ), 2 ) avg_replacement_cost
FROM
film
INNER JOIN film_category USING(film_id)
INNER JOIN category USING(category_id)
WHERE
category_id = 7;

Here is the result:

PostgreSQL aggregate functions - Advanced AVG example

COUNT() function examples

To get the number of films, you use the COUNT(*) function as follows:

1
2
3
4
SELECT
    COUNT(*)
FROM
    film;

Here is the output:

PostgreSQL aggregate functions - Basic COUNT example

To get the number of drama films, you use the following statement:

1
2
3
4
5
6
7
8
SELECT
COUNT(*) drama_films
FROM
film
INNER JOIN film_category USING(film_id)
INNER JOIN category USING(category_id)
WHERE
category_id = 7;

The result showed that we have 62 drama films:

PostgreSQL aggregate functions - COUNT example

MAX() function examples

The following statement returns the maximum replacement cost of films.

1
2
3
4
SELECT
    MAX(replacement_cost)
FROM
    film;

PostgreSQL aggregate functions - Basic MAX function example

To get the films that have the maximum replacement cost, you use the following query:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SELECT
film_id,
title
FROM
film
WHERE
replacement_cost =(
SELECT
MAX( replacement_cost )
FROM
film
)
ORDER BY
title;

PostgreSQL aggregate functions - MAX example

The subquery returned the maximum replacement cost which then was used by the outer query for retrieving the film’s information.

MIN() function examples

The following example uses the MIN() function to return the minimum replacement cost of films:

1
2
3
4
SELECT
    MIN(replacement_cost)
FROM
    film;

To get the films which have the minimum replacement cost, you use the following query:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
SELECT
film_id,
title
FROM
film
WHERE
replacement_cost =(
SELECT
MIN( replacement_cost )
FROM
film
)
ORDER BY
title;

PostgreSQL aggregate functions - MIN function subquery example

SUM() function examples

The following statement uses the SUM() function to calculate the total length of films grouped by film’s rating:

1
2
3
4
5
6
7
8
9
SELECT
rating,
SUM( rental_duration )
FROM
film
GROUP BY
rating
ORDER BY
rating;

The following picture illustrates the result:

PostgreSQL aggregate functions - SUM example

In this tutorial, you have learned about PostgreSQL aggregate functions and apply them to summarize data.

Next Tutorial: PostgreSQL AVG Function

PostgreSQL Quick Start

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

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

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

  • PostgreSQL String Functions
  • PostgreSQL SPLIT_PART
  • PostgreSQL CHR
  • PostgreSQL ASCII
  • An Overview of PostgreSQL Aggregate Functions
  • PostgreSQL POSITION
  • PostgreSQL MD5
  • PostgreSQL REGEXP_MATCHES
  • PostgreSQL REGEXP_REPLACE
  • PostgreSQL FORMAT

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.