Difficulty Level : Basic
In this article, we will discuss how to use the PostgreSQL Max() Function.
This function is a built-in operation.
Let’s dive into why this function exists and how to use it in both Code & No Code.
Summary
What is the PostgreSQL Max() Function?
PostgreSQL MAX() function is an aggregate function that returns the maximum value in a given set of values.
Case 1: PostgreSQL MAX() function without any clause
It returns the highest value in the selected column.
Syntax
SELECT Max(Column_A)
FROM Table_1
Case 2: PostgreSQL MAX() function with multiple columns
It returns the highest value in the selected columns with a view by rows.
Syntax
SELECT Max(Column_A, Column_B)
FROM Table_1
Case 3: PostgreSQL MAX() function with Group By
The following code will demonstrate the maximum value from the “name” column and group the results by Column B.
Syntax
SELECT Max(Column_A) as Column_A, Column_B
FROM Table_1
Group by Column_B
How to Use PostgreSQL Max() Function: The Hard Way With Code
Dataset
Let’s take a look at the following Facebook Public Sample Dataset (available to use for free).
Case 1: PostgreSQL MAX() function without any clause
Example
- Input
SELECT Max (Clicks)
FROM Facebook_Datasets
- Output
1840
Case 2: PostgreSQL MAX() function with multiple columns
Example
- Input
SELECT Max (Clicks, Reach, Reactions)
FROM Facebook_Datasets
- Output
15647
Case 3: PostgreSQL MAX() function with Group By
The following example will demonstrate the maximum value from the “name” column and group the results by Gender(Male/Female).
Example
- Input
SELECT Max (Clicks) as Clicks, Type
FROM Facebook_Datasets
Group By Type
- Output
Link | 2899
Video | 7391
Photo | 10299
How to use PostgreSQL Max() Function: The Easy Way Without Coding
Syntax in RestApp:
Prerequisite:
It is required to have your data type in numeric format for Max() operation to function. If not sure about the format of your columns, you can use the cast() function.
Dataset
Let’s take a look at the following Facebook Public Sample Dataset (available to use for free).
Case 1: PostgreSQL MAX() function with Multiple Columns
Example
Beforehand, we need to Cast() the columns on which Max() Function will be operated: “Reach” and “Clicks”:
- Input
- Output
Case 2: PostgreSQL MAX() function with Group By
Syntax
Example
- Input
- Output
Conclusion
At RestApp, we’re building a Data Activation Platform for modern data teams.
We have designed our next-gen data modeling editor to be intuitive and easy to use.
If you’re interested in starting with connecting all your favorite tools, check out the RestApp website or try it for free with a sample dataset.
Discover the next-gen end-to-end data pipeline platform with our built-in No Code SQL, Python and NoSQL functions. Data modeling has never been easier and safer thanks to the No Code revolution, so you can simply create your data pipelines with drag-and-drop functions and stop wasting your time by coding what can now be done in minutes!
Discover Data modeling without code with our 14-day free trial!
Category
Subscribe to our newsletter
Related articles
SQL
Top 5 SQL challenges preventing you from mastering it – Feedback from 100+ Data professionals!
Brian Laleye
July 19, 2022
· 12 min read
NO CODE / LOW CODE, SQL
What are Window functions and How to use them properly in Low/No-Code? A full guide with Examples
Brian Laleye
June 28, 2022
· 8 min read
AUTOMATION, NO CODE / LOW CODE, SQL
Top 10 SQL functions to clean your data in No Code. A Complete Guide with Examples
Othmane Lamrani
May 3, 2022
· 13 min read
Build better data pipelines
With RestApp, be your team’s data hero by activating insights from raw data sources.