PostgreSQL MAX() Function (original) (raw)

Last Updated : 15 Jul, 2025

The **MAX() function in PostgreSQL is a versatile and powerful **aggregate function used to determine the **maximum value within a set of values. It plays a crucial role in **data analysis, **reporting, and **SQL query optimization. This function can be applied in **SELECT, **WHERE, **GROUP BY, and **HAVING clauses, making it indispensable for analyzing data efficiently.

In this article, we will explain the **PostgreSQL MAX() function, understand its **syntax, explore **examples with outputs, and highlight its **important features to help us understand its full potential.

What is PostgreSQL MAX() Function?

The MAX() function in PostgreSQL retrieves the **highest value from a specified column or expression. It works with **numeric, **date, or **text data types and ignores NULL values in computations. This function is often paired with **GROUP BY, **HAVING, or **WHERE clauses for more complex queries.

**Syntax

MAX(expression)

**expression: The column or calculated expression from which we want to find the **maximum value.

PostgreSQL MAX() Function Examples

Let us take a look at some **practical examples of the **MAX() function in PostgreSQL to better understand its **functionality and **use cases. These examples will demonstrate how the function can be applied to different scenarios, such as finding the **highest value in a **column, grouping results, and applying conditions to our **queries for more refined results.

**Example 1: Finding the Maximum Payment Amount

The following query retrieves the highest payment amount recorded in the payment table. It ensures that only the largest value is retrieved, ignoring any NULL entries.

**Query:

SELECT MAX(amount)
FROM payment;

**Output

Output: Example 1: Finding the Maximum Payment Amount

**Explanation:

This query scans the '**amount'column in the 'payment'**table and returns the maximum value, which represents the highest payment made by any **customer.

**Example 2: Maximum Payment Per Customer

The next example demonstrates how to find the *largest payment made by each customer. This involves grouping the results by '*customer_id'. **This allows us to see the **maximum payment made by every individual customer in the **dataset.

**Query:

SELECT
customer_id,
MAX (amount)
FROM
payment
GROUP BY
customer_id;

**Output

PostgreSQL MAX() Function Example

**Explanation:

In this query, '**customer_id'**is used to group the payments. The MAX(amount) function finds the highest payment for each customer within these groups.

Important Points About PostgreSQL MAX() Function

Optimizing Queries with MAX()

Conclusion

The **PostgreSQL MAX() function is an essential tool for analyzing data and identifying **maximum values efficiently. By combining it with GROUP BY, **WHERE, and **HAVING clauses, we can create powerful queries for **data aggregation and **business requirements. Whether we're working with financial data, timestamps, or text, the **MAX() function is a go-to choice for retrieving maximum values **quickly and **accurately.