w3resource

MySQL MAX() function

MAX() function

MySQL MAX() function returns the maximum value of an expression.

This function is useful in -

  • To identify the highest value within a column is crucial for understanding the upper limit of a dataset.
  • When analyzing data, you might want to know the highest or peak value in a certain attribute.
  • The MAX() function helps evaluate the highest performance metrics, such as maximum revenue, profits, or user engagement.
  • Identifying the maximum value helps in assessing whether a process is operating within acceptable limits.
  • When comparing performance against benchmarks, the MAX() function assists in determining how close or far data is from the best achieved results.
  • In e-commerce or inventory management, determining the maximum order quantity helps in fulfilling customer demands efficiently.
  • For critical decisions, knowing the maximum value in relevant data attributes can provide insights for strategic planning.

Syntax:

MAX(expr);

Where expr is an expression.

MySQL Version: 8.0

Example:

Sample table: book_mast

+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
| book_id | book_name                           | isbn_no     | cate_id | aut_id | pub_id | dt_of_pub  | pub_lang | no_page | book_price |
+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+
| BK001   | Introduction to Electrodynamics     | 0000979001  | CA001   | AUT001 | P003   | 2001-05-08 | English  |     201 |      85.00 |
| BK002   | Understanding of Steel Construction | 0000979002  | CA002   | AUT002 | P001   | 2003-07-15 | English  |     300 |     105.50 |
| BK003   | Guide to Networking                 | 0000979003  | CA003   | AUT003 | P002   | 2002-09-10 | Hindi    |     510 |     200.00 |
| BK004   | Transfer  of Heat and Mass          | 0000979004  | CA002   | AUT004 | P004   | 2004-02-16 | English  |     600 |     250.00 |
| BK005   | Conceptual Physics                  | 0000979005  | CA001   | AUT005 | P006   | 2003-07-16 | NULL     |     345 |     145.00 |
| BK006   | Fundamentals of Heat                | 0000979006  | CA001   | AUT006 | P005   | 2003-08-10 | German   |     247 |     112.00 |
| BK007   | Advanced 3d Graphics                | 0000979007  | CA003   | AUT007 | P002   | 2004-02-16 | Hindi    |     165 |      56.00 |
| BK008   | Human Anatomy                       | 0000979008  | CA005   | AUT008 | P006   | 2001-05-17 | German   |      88 |      50.50 |
| BK009   | Mental Health Nursing               | 0000979009  | CA005   | AUT009 | P007   | 2004-02-10 | English  |     350 |     145.00 |
| BK010   | Fundamentals of Thermodynamics      | 0000979010  | CA002   | AUT010 | P007   | 2002-10-14 | English  |     400 |     225.00 |
... ... ...
+---------+-------------------------------------+-------------+---------+--------+--------+------------+----------+---------+------------+

View the table

Code:


-- This SQL query retrieves the maximum value of the book price from the book_mast table.
SELECT MAX(book_price) -- Selects the maximum value of the book price
FROM book_mast; -- Specifies the table from which to retrieve data (book_mast table)

Explanation:

  • This SQL query retrieves data from the book_mast table.

  • It selects the maximum value of the book price.

  • Here's how the process works:

    • The query uses the MAX() function to find the maximum value of the book price column.

    • It retrieves this value from the book_mast table.

    • Finally, the query returns the maximum value of the book price.

Relational Algebra Expression:

Relational Algebra Expression: MAX() function.

Relational Algebra Tree:

Relational Algebra Tree: MAX() function.

Explanation:

The above MySQL statement will return the maximum 'book_price' from 'book_mast' table.

Output:

mysql> SELECT MAX(book_price)
    -> FROM book_mast;
+-----------------+
| MAX(book_price) |
+-----------------+
|          250.00 | 
+-----------------+
1 row in set (0.00 sec)

PREV : GROUP_CONCAT()
NEXT : Max() with group by



Follow us on Facebook and Twitter for latest update.