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 ExamplesLet 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 AmountThe 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
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.
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
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.
MAX()
function computes the maximum value over a specified set of values, which can be an entire table or a subset defined by a WHERE
clause.MAX()
can be effectively combined with GROUP BY
to find maximum values within groups, and with HAVING
to filter groups based on aggregate conditions.MAX()
operates on a single column, you can include multiple MAX()
functions in a single query to find maximum values of different columns.MAX()
ignores NULL
values in the computation.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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4