T-SQL » Functions » MAX

Syntax:
SELECT MAX ( field ) [ AS expression ]
FROM table
[ WHERE criteria ]
[ GROUP BY grouping_expression ]
[ HAVING criteria ]
field
The name of the column where the maximum of the values is to be found.
expression
The name to be displayed for the resulting maximum value.
table
The name of the table the information is being retrieved from.
criteria
The condition(s) that dictate which rows are to be examined to determine the maximum value.
grouping_expression
An optional parameter that specifies a set of columns on which to group matching data.

The MAX function simply finds the highest, or maximum, value in a field.

Examples

Code:
SELECT MAX(DaysInHospital) AS [MaxStay]
FROM MediCarePatients
WHERE age > 65;
Output:
MaxStay
214

(1 row(s) affected)
Explanation:

This code finds the longest hospital stay by MediCare patients over the age of 65 and stores that information in 'MaxStay'.

Language(s): MS SQL Server

See Also: