Updated May 16, 2023
Introduction to PostgreSQL MAX
PostgreSQL max function is an aggregate function basically used to return the maximum value from the set of values in PostgreSQL. Max function is used in many applications like finding the maximum salary of the employee or the highest mark of the student, max function is very important and useful to find the maximum value from the number of values. We can use the max function in select as well as where and having clauses, max function in PostgreSQL will return the maximum value from the full column value, this function will accept the numeric value as column input and return the max number as output.
Syntax
Below is the syntax of the max function:
Max (column_name (Name of column which was used with max function to get the maximum value from the set of values.))
OR
Select column_name1, column_name2, …, max (column_name or aggregate expression) from table_name [ where condition ]group by column_name;
OR
Select max (column_name or aggregate expression) from table_name [ where condition]
OR
Max (Distinct column_name (Name of column which was used with max function to get the maximum value from the set of values.))
Parameters
Below is the parameter description syntax of the max function:
- Max: Use this function to obtain the maximum values from all column values in PostgreSQL.When we want the maximum number from the set of values simultaneously, we use a max function in PostgreSQL.
- Distinct: This keyword is used to get the maximum number from distinct values from a numeric column in PostgreSQL. It is an optional parameter of a max function in PostgreSQL.
- Column name: It is defined as the column’s name on which we have performed operations to find the max value from the column. The column name is a very important parameter in the max function.
- Table name: It is defined as the name of the table from which column we have performing the malfunction operation and get the result to find maximum values from all the values. We can use a numeric column table name to use the max function in PostgreSQL.
- Select: This operation is used to retrieve data from the table. We have to retrieve data using a max function in PostgreSQL. We have used the max function using select operations.
- Where condition: We have filtered the result of select operations in PostgreSQL using the where condition. We have got specific results by selecting conditions using PostgreSQL’s where condition and max function.
- Group by: We have used group by clause with max function in PostgreSQL. We have to retrieve results as per group by condition using a max function in PostgreSQL.
How does MAX function work in PostgreSQL?
Below is the working of the max function in PostgreSQL. Basically, we have used the max function to get the maximum number from a set of values.
Max’s function is the aggregate function of PostgreSQL. Max’s function is to retrieve the single results from multiple values in PostgreSQL. Using a distinct clause with the max function will only get the maximum number from the distinct values.
In the below example, we have used the max function with a distinct clause, it will only return the maximum number result of a column which contains only the distinct values.
Code:
select * from emp_test;
select max(distinct salary) from emp_test;
Output:
Explanation: In the above example, we must retrieve the maximum value from the salary column. We have retrieved value from the salary column using a distinct clause. In order to extract the maximum values from the column, we have utilised the max function. We used the max function with the distinct, group by, having clause, and where condition in PostgreSQL.
PostgreSQL max function will take value from a single as well as multiple columns to form a valid mathematical expression from this expression max function will return the maximum values from the column.
Max function will exclude the null values from the column if the column contains the null value, and we have to perform the max function operation on the same then, the max function will exclude the null values from the column.
After excluding null values, it will provide the result of the max function. The below example shows that the max function will exclude the null values from the column.
Code:
select * from emp_test;
select max(salary) from emp_test;
Output:
Explanation: In the above example, we have to perform max function operation on the salary column, after performing max function operation on the salary column it will return the maximum number from the salary column.
Examples To Implement PostgreSQL MAX
Examples are mentioned below:
We have used emp_test to describe the example of a max function in PostgreSQL is as follows.
Below are the data and table descriptions of the emp_test table.
Code:
select * from emp_test;
\d+ emp_test;
Output:
Example #1: Max function without using any clause
In the example below, we have used the max function without any clause. We have applied a max function on the salary column.
Code:
select max (salary) from emp_test;
Output:
Example #2: Max function using distinct clause
We used the max function with a distinct clause in the example below. We have applied the max function with a distinct clause on the salary column.
Code:
select max (distinct salary) from emp_test;
Output:
Example #3: Max function using group by clause
In the example below, we use a max function with the group by clause. We have applied a max function on the salary column and a group by clause on the emp_name column.
Code:
select emp_name, max (salary) from emp_test group by emp_name;
Output:
Example #4: Max function using where the condition
In the below example, we have used the max function with the where condition. We have applied a max function on the salary column and a where condition on the emp_name column.
Code:
select max (salary) from emp_test where emp_name = 'ABC';
Output:
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL MAX” was beneficial to you. You can view EDUCBA’s recommended articles for more information.