<< Click to Display Table of Contents >> Navigation: Apollo SQL > Aggregate Functions > MAX |
Calculates the largest value for a column.
Syntax:
MAX([ALL] column_reference | DISTINCT column_reference)
As an aggregate function, MAX performs its calculation aggregating values in the same column(s) across all rows in a dataset. The dataset may be the entire table, a filtered dataset, or a logical group produced by a GROUP BY clause. Column values of zero are included in the aggregation. NULL column values are not counted in the calculation. If the number of qualifying rows is zero, MAX returns a NULL value.
Example:
SELECT MAX(itemstotal) FROM orders
ALL returns the largest value for all rows. When DISTINCT is not specified, ALL is the implied default. DISTINCT ignores duplicate values when calculating the largest value in the specified column. MAX returns the largest value in a column or a calculation using a column performed for each row (a calculated field).
Example:
SELECT MAX(itemstotal), MAX(itemstotal * 0.0825) AS HighestTax FROM orders
When used with a GROUP BY clause, MAX returns one calculation value for each group. This value is the aggregation of the specified column for all rows in each group. The statement below aggregates the largest value for the order totals column in the ORDERS table, producing a subtotal for each company in the COMPANY table.
Example:
SELECT C.company, AVG(O.itemstotal) AS Average,
MAX(O.itemstotal) AS Biggest, MIN(O.itemstotal) AS Smallest
FROM customer C, order O
WHERE (C.custno = O.custno)
GROUP BY C.company
ORDER BY C.company
MAX can be used with all non-BLOB columns. When used with numeric columns, the return value is of the same type as the column (such as INTEGER or FLOAT).
The MAX function cannot be used with memo or BLOB columns.