PostgreSQL Tutorial

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

PostgreSQL MAX Function

Summary: this tutorial shows you how to use the PostgreSQL MAX function to get maximum value of a set.

Introduction to PostgreSQL MAX function

PostgreSQL  MAX function is an aggregate function that you use to get the maximum value in a set. The MAXfunction is useful in many cases, for example to find the employees with the highest salary, most expensive products, etc.

The syntax of the MAX function is as follows:

1
SELECT MAX(expression)

You can use the MAX function not only in the SELECT clause but also in the WHERE and HAVING clauses. Let’s take a look at several examples of using the MAX function.

PostgreSQL MAX function examples

Let’s examine the paymenttable.

payment table

The following query gets the maximum amount paid by customers from the paymenttable:

1
2
SELECT MAX(amount)
FROM payment;

PostgreSQL MAX function example

PostgreSQL MAX function in subquery

To get other information together with the maximum payment, you use a subquery as follows:

1
2
3
4
5
6
7
8
9
10
11
SELECT
   *
FROM
   payment
WHERE
  amount = (
SELECT
      MAX (amount)
FROM
   payment
);

First, the subquery gets the maximum payment and then the outer query selects all rows with the amount that is equal to the maximum payment returned from the subquery. The output of the query is as follows:

PostgreSQL MAX with Subquery

The following diagram illustrates the steps that PostgreSQL performs the query:

postgresql max function with subquery

PostgreSQL MAX function with GROUP BY clause

You can also combine the MAXfunction with GROUP BY clause to get the maximum value per group. For example, the following query gets the largest payment paid by each customer for all customers.

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

PostgreSQL max function with group by

PostgreSQL MAX with GROUP BY example

PostgreSQL MAX function with HAVING clause

If you use the  MAXfunction in a HAVING clause, you can apply a filter for the whole group. For example, the following query selects only the biggest payment paid by each customer and the biggest payments are greater than  8.99.

1
2
3
4
5
6
7
8
SELECT
customer_id,
MAX (amount)
FROM
payment
GROUP BY
customer_id
HAVING MAX(amount) > 8.99

PostgreSQL max function with group by

PostgreSQL MAX with HAVING example

Finding largest values from two or more columns

Let’s create a new table named ranks for the demonstration. It has four columns: 1 column to store user id and other three columns to store rank from 1 to 3.

1
2
3
4
5
6
CREATE TABLE IF NOT EXISTS ranks (
user_id INT PRIMARY KEY,
rank_1 int4 NOT NULL,
rank_2 int4 NOT NULL,
rank_3 int4 NOT NULL
);

We can insert sample data into the ranks table as follows:

1
2
3
4
5
INSERT INTO ranks
VALUES
(1, 6, 3, 5),
(2, 2, 8, 5),
(3, 5, 9, 8);

How do we get the largest rank per user:

postgresql greatest function

To achieve this, you use GREATEST function instead of MAX function. The GREATEST function returns the greatest value from a list of values.

1
2
3
4
5
SELECT
user_id,
GREATEST (rank_1, rank_2, rank_3) AS largest_rank
FROM
ranks;

In this tutorial, you have learned how to use the PostgreSQL  MAX function to find the maximum value of an expression. In addition, you learned how to use the  MAX function with  GROUP BY and HAVINGclauses.

Related Tutorials

  • PostgreSQL MIN Function
  • PostgreSQL AVG Function
  • PostgreSQL SUM Function
  • PostgreSQL COUNT Function
Previous Tutorial: Using PostgreSQL CAST To Convert From One Data Type Into Another
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 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

  • PostgreSQL Recursive View
  • Learn PostgreSQL Recursive Query By Example
  • Creating Updatable Views Using the WITH CHECK OPTION Clause
  • PostgreSQL Upsert Using INSERT ON CONFLICT statement
  • How to Generate a Random Number in A Range
  • Using PostgreSQL ADD COLUMN to Add One or More Columns To a Table
  • PostgreSQL Character Types: CHAR, VARCHAR, and TEXT
  • Using PostgreSQL SERIAL To Create Auto-increment Column
  • PostgreSQL Boolean Data Type with Practical Examples
  • Understanding PostgreSQL Timestamp Data Types

More Tutorials

  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Functions
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

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