The MAX() function is used to retrieve the maximum value from a set of values in a column or expression.
Syntax
MAX(expression)
Definition and Usage
- The
MAX()function returns the highest value in a specified column or result set. - It works on both numeric and string data types.
- When used on strings, the function returns the value that is highest in alphabetical order.
- Note: NULL values are ignored.
Parameter Values
| Parameter | Description |
|---|---|
expression |
Required. A column name, numeric value, or formula. |
Technical Details
- Introduced in: MySQL 4.0
- Return Value: The maximum value from the specified column or expression.
Examples
Example 1: Maximum price in a table
Find the highest product price in the Products table:
SELECT MAX(Price) AS LargestPrice FROM Products;
Output:
LargestPrice
------------
500.00
Example 2: Maximum value in a numeric column
Retrieve the highest age from the Employees table:
SELECT MAX(Age) AS OldestAge FROM Employees;
Output:
OldestAge
---------
65
Example 3: Maximum value in a string column
Find the last name that is alphabetically last:
SELECT MAX(LastName) AS LastAlphabeticalName FROM Customers;
Output:
LastAlphabeticalName
---------------------
Zuckerberg
Related Functions
MIN(): Returns the minimum value in a set of values.AVG(): Returns the average of a set of numeric values.SUM(): Calculates the total sum of a numeric column.
Notes
- The
MAX()function can be combined with other SQL clauses such asGROUP BYandHAVINGto refine results. - It is commonly used in queries to identify extreme values or to extract specific records.