The Code Developer

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;
BookIdBookNamePriceAuthor
1Learning PHP, MySQL, and JavaScript17Robin Nixon
2Ubuntu: Up and Running23Robin Nixon
3PHP and MySQL Web Development12Luke Welling
4Murach's PHP and MySQL14Joel Murach
5Murach's Java Programming62Joel Murach
6Head first php mysql22Lynn Beighley
7Head first sql11Lynn Beighley
8HTML5 for IOS and Android: A Beginner's Guide4Robin Nixon

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