EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL MAX
 

PostgreSQL MAX

Priya Pedamkar
Article byPriya Pedamkar

Updated May 16, 2023

PostgreSQL MAX

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

PostgreSQL MAX1

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:

PostgreSQL MAX2

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:

PostgreSQL MAX3

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:

any clause

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:

distinct clause

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:

group by clause

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:

where condition

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.

  1. PostgreSQL RANDOM
  2. PostgreSQL Alias
  3. HAVING PostgreSQL
  4. Sequence in PostgreSQL

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW