## 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 highest mark of the student, max function is very important and useful to find 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 full column value, this function will accept the numeric value as column input and return 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 max function:

**Max:**This function is used to get the maximum values from all column values in PostgreSQL. When we want the maximum number from the set of values at the same time we have using 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 name of the column on which we have performed operations to find 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 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 max function using select operations.**Where condition:**Using where condition we have filtered the result of select operations in PostgreSQL. We have got specific results by selecting conditions using where condition and max function in PostgreSQL.**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 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. If we have used a distinct clause with the max function it will only get the maximum number from the distinct values.

In the below example we have used 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 have to retrieve the maximum value from the salary column. We have retrieved value from the salary column using a distinct clause. We have used the max function where we have to retrieve max values from the column. We have used max function with the distinct, group by, having clause and with where condition in PostgreSQL.

PostgreSQL max function will take value from a single as well as multiple columns to form valid mathematical expression from this expression max function will return the maximum values from the column.

2 Online Courses | 1 Hands-on Project| 7+ Hours| Verifiable Certificate of Completion

4.5

View Course

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 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

Below are the examples mentioned:

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 below example, we have used max function without using 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

In the below example, we have used the max function with a distinct clause. 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 below example, we have using a max function with the group by clause. We have applied a max function on the salary column and 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 condition

In the below example, we have used max function with where condition. We have applied a max function on the salary column and where condition on the emp_name column.

**Code:**

`select max (salary) from emp_test where emp_name = 'ABC';`

**Output:**

### Recommended Articles

This is a guide to PostgreSQL MAX. Here we discuss an introduction to PostgreSQL MAX, how does it work with query examples. You can also go through our other related articles to learn more –