SQL MAX() function is used to return maximum value from a column of a table using a SELECT statement in sql.

SQL MAX function syntax

Syntax:
SELECT MAX([COLUMN_NAME]) FROM [TABLE_NAME]

MAX function Query Example:
To get the maximum price of the book from a BOOKS table, we need use Max function on PRICE column as below.

SELECT MAX(PRICE) FROM BOOKS;

SQL MAX function with Group By

SQL MAX() function syntax  when grouping the results by one or more columns.

Syntax:

SELECT  [COLUMN1],[COLUMN2],  ……MAX[COLUMN_N]
FROM [TABLE_NAME]  [WHERE CONDITION]
GROUP BY COLUMN1,COLUMN2……[COLUMNN]

SQL Max() Example with group by

Let’s say we want to get maximum price of a book titled “The Castle “from the BOOKS table and there are multiple books with same title with different prices also.

TITLEPRICE
Historica98
The Castle100
The Castle150
Animal Farm300
Animal Farm120

So, the query below will find the max price of books of same titles

Query example:
SELECT TITLE, MAX(PRICE) FROM BOOKS GROUP BY TITLE, PRICE;

Output

TITLEPRICE
Historica98
The Castle150
Animal Farm300

Related Posts