/ ID: 793 Title: Use of Max function in Postgres SQL 793 Meta Description: This tutorial shows why and how we use the Max function in Postgres SQL. Code samples included. Meta Keywords: Max Function Postgres SQL Author: orkb Template: Unstructured Tutorial Categories: PostgreSQL Tags: Max, Function, PostgreSQL, SQL, Group by Status: Published /
In this article, we'll learn to use the aggregate Max function in our Postgres SQL commands from multiple perspectives, to gain a deeper level of understanding. Here we will discuss:
The Postgres MAX() function returns the highest (maximum) value from a set of numbers.
Syntax
SQL
i_highest_of_set := MAX(set_of_numbers);
Any time you have a set of numbers where you want to know which of that set is the highest in set.
We'll begin with an easy-to-understand example. Our initial dataset is a table called coding_languages.
| id_tech | t_name | i_rapid_dev | |:-------:|------------|------------:| | 0 | Python | 7 | | 1 | Java | 5 | | 2 | PHP | 9 | | 3 | Javascript | 8 |
At our software development company, the above table called coding_languages has three fields, including:
The goal is to find out which language has the highest rating in terms of rapid development (i_rapid_dev). Let's write our SQL script to use the MAX function to retrieve the t_name that has the highest value in the i_rapid_dev column in our PostgreSQL database recordset:
SQL
SELECT
id_tech
, t_name
, MAX(i_rapid_dev)
FROM
coding_languages
GROUP BY
i_rapid_dev;
Note for the SQL example above: This is where we use our knowledge of SQL GROUPs. You can think of it this way: If you want to find the maximum of more than one number, you need to consider a set of numbers.
How does the SQL statement above work? We'll take it step by step:
The above query returns: | id_tech | t_name | i_rapid_dev | |:-------:|--------|------------:| | 2 | PHP | 9 |
We now know that PHP is the fastest in terms of rapid development.
The mini project: We had employees grade the technologies used at our company and want to know the highest graded tech. Here we will explore another relatively simple use of MAX() while introducing the "HAVING" key word. Here's our "raw" dataset with the table name of "technologies":
| id_tech | t_name_user | t_name_tech | t_category_tech | i_grade | |--------:|-------------|-------------|-----------------|--------:| | 0 | Ted | Java | Lang | 4 | | 1 | Bif | Mongo | NoSQL | 16 | | 2 | Susan | MySQL | RDB | 8 | | 3 | Tim | PostgreSQL | RDB | 16 | | 4 | Gary | Python | Lang | 20 | | 5 | Sammy | PHP | Lang | 12 |
Let's plan how to proceed. We want to build a query that will do more than one thing, including:
SQL
SELECT
t_name_tech
, SUM(i_grade) as i_sum_of_grades
, MAX(i_sum_of_grades) AS i_highest_grade
FROM
coding_languages
GROUP BY
t_name_tech
, i_grade;
HAVING
t_category_tech = 'Lang'
Analysis of the SQL above:
The query above returns:
| t_name_tech | t_category_tech | i_grade | |-------------|-----------------|--------:| | PHP | Lang | 12 |
Here we learned when and how it is best to use the MAX() function in PostgreSQL to find the highest valued number in a set of numbers. We used both beginner-level and more difficult examples, scaling up difficulty, so as to make the learning process as easy as possible because SQL can at times be difficult to wrap your head around, especially when you use the GROUP BY and HAVING clauses to work with subsets of data.