MAX()
Returns the largest value in a column across a group of rows.
Description
MAX is an aggregate function that returns the largest value found in a
column across the rows being aggregated. It works on numbers (highest
salary), dates (most recent hire date), and even text (alphabetically last
value), making it more versatile than it first appears.
Syntax
MAX(column_name)Parameters
| Name | Description | Optional |
|---|---|---|
| expression | The column or expression to evaluate. NULL values are ignored when finding the maximum. | No |
Return Type
MAX returns the same type as the column it's applied to: a numeric column
returns a number, a date column returns a date, and so on.
Examples
Loading database engine...
Loading database engine...
MAX also works on dates and text
MAX(hire_date) returns the most recent hire date, and MAX(last_name)
returns the last name that sorts last alphabetically. MAX isn't limited
to numbers.
Common Mistakes
- Assuming
MAXonly works on numbers. It also works on dates, text, and other orderable types, which is easy to forget. - Using
MAXto find the whole row with the highest value.MAX(salary)only returns the salary number, not which employee earns it. You need a separate query (like ordering and limiting, or a subquery) to get the full row. - Forgetting
GROUP BYwhen a per-category maximum is intended. Without it,MAXcollapses the entire table into a single overall maximum.
Related Functions
See also: MIN, AVG, SUM.