![]() ![]() For example, the following query gets the highest payment paid by each customer. ![]() You can combine the MAX function with the GROUP BY clause to get the maximum value for each group. The following diagram illustrates the steps that PostgreSQL performs the query: PostgreSQL MAX function with GROUP BY clause ) Code language: SQL (Structured Query Language) ( sql )įirst, the subquery uses the MAX() function to return the highest payment and then the outer query selects all rows whose amounts are equal the highest payment returned from the subquery. To get other information together with the highest payment, you use a subquery as follows: SELECT * FROM payment The following query uses the MAX() function to find the highest amount paid by customers in the payment table: SELECT MAX(amount)įROM payment Code language: SQL (Structured Query Language) ( sql ) PostgreSQL MAX function in subquery Let’s examine the payment table in the sample database. Let’s take a look at some examples of using the MAX function. You can use the MAX function not only in the SELECT clause but also in the WHERE and HAVING clauses. The syntax of the MAX function is as follows: MAX(expression) Code language: SQL (Structured Query Language) ( sql ) For example, you can use the MAX function to find the employees who have the highest salary or to find the most expensive products, etc. The MAX function is useful in many cases. PostgreSQL MAX function is an aggregate function that returns the maximum value in a set of values. ![]() Summary: this tutorial shows you how to use the PostgreSQL MAX() function to get the maximum value of a set. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |