MAX is a standard SQL aggregate and analytic (window) function that scans a collection of values and returns the highest non-NULL value it encounters. When used as an aggregate, it collapses multiple input rows into a single output row per grouping set. When paired with an OVER() clause, MAX becomes a window function that supplies the maximum value relative to the current row's window frame while preserving row granularity.Key points:- Operates on numeric, date/time, and string data types that support ordering.- Ignores NULL values. If all values are NULL, the result is NULL.- Accepts the DISTINCT modifier in some dialects, though it is redundant because the maximum of a set equals the maximum of its distinct set.- Works inside HAVING, ORDER BY, SELECT, and subqueries but cannot appear directly in a WHERE clause without a subquery because aggregates are evaluated after filtering.- Window usage supports PARTITION BY, ORDER BY, ROWS/RANGE frames, enabling running or sliding maximum calculations.- Collation rules dictate string ordering. Be cautious when comparing character data across different collations.- Performance depends on indexing and grouping. Aggregate MAX on an indexed column can be optimized to O(1) in many databases.
expression
(Any comparable data type) - Value to evaluate for maximum.DISTINCT
(Keyword (optional)) - Eliminates duplicates before evaluation.window_clause
(Window definition) - Defines partitions and frame for window MAX.MIN, SUM, AVG, COUNT, GROUP BY, HAVING, WINDOW FUNCTIONS, ORDER BY
SQL-92
No. The function skips NULLs. If every row is NULL, the result is NULL.
Group by department and apply MAX:SELECT department_id, MAX(salary) AS top_salary FROM employees GROUP BY department_id;
Not directly. Use a subquery or HAVING. Example:SELECT * FROM orders WHERE total_amount = (SELECT MAX(total_amount) FROM orders);
Almost never. MAX of a set equals the MAX of its distinct values, so DISTINCT is redundant unless required for syntax consistency.