MySQL MAX Function

Introduction to MySQL MAX Function

This MySQL tutorial explains how to use the MAX function with syntax and examples. The MySQL MAX function returns the maximum value in a set of values.

MAX() Syntax

SELECT MAX(column_name)
FROM table_name
WHERE condition;

To understand MAX function, consider an “books” table, which is having the following records:

SELECT * FROM books;
BookIdBookNamePriceAuthorpublishedDate
1Learning PHP, MySQL, and JavaScript17Robin Nixon2017-02-02
2Ubuntu: Up and Running23Robin Nixon2017-03-23
3PHP and MySQL Web Development12Luke Welling2017-06-14
4Murach's PHP and MySQL14Joel Murach2017-06-17
5Murach's Java Programming62Joel Murach2017-07-28
6Head first php mysql22Lynn Beighley2017-07-31
7Head first sql11Lynn Beighley2017-09-10
8HTML5 for IOS and Android: A Beginner's Guide4Robin Nixon2017-09-12

The following MySQL statement finds the price of the most expensive book:

SELECT MAX(Price) AS MaxPrice
FROM books;
MaxPrice
62

The following MySQL statement finds the price of the most expensive book in each author, sorted high to low:

SELECT MAX(Price) AS MaxPrice, Author
FROM books
GROUP BY Author
ORDER BY MaxPrice DESC;
MaxPriceAuthor
62Joel Murach
23Robin Nixon
22Lynn Beighley
12Luke Welling

Leave A Reply

Your email address will not be published.