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;
BookId | BookName | Price | Author | publishedDate |
---|---|---|---|---|
1 | Learning PHP, MySQL, and JavaScript | 17 | Robin Nixon | 2017-02-02 |
2 | Ubuntu: Up and Running | 23 | Robin Nixon | 2017-03-23 |
3 | PHP and MySQL Web Development | 12 | Luke Welling | 2017-06-14 |
4 | Murach's PHP and MySQL | 14 | Joel Murach | 2017-06-17 |
5 | Murach's Java Programming | 62 | Joel Murach | 2017-07-28 |
6 | Head first php mysql | 22 | Lynn Beighley | 2017-07-31 |
7 | Head first sql | 11 | Lynn Beighley | 2017-09-10 |
8 | HTML5 for IOS and Android: A Beginner's Guide | 4 | Robin Nixon | 2017-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;
MaxPrice | Author |
---|---|
62 | Joel Murach |
23 | Robin Nixon |
22 | Lynn Beighley |
12 | Luke Welling |