EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL MAX
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

PostgreSQL MAX

By Priya PedamkarPriya Pedamkar

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

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,700 ratings)

OR

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

PostgreSQL MAX1

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.

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:

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

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:

PostgreSQL MAX3

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:

any clause

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:

distinct clause

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:

group by clause

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:

where condition

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 –

  1. PostgreSQL RANDOM
  2. PostgreSQL Alias
  3. PostgreSQL HAVING
  4. Sequence in PostgreSQL
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more