MAX() in SQL
Returns the largest value from a specified column or expression, ignoring NULL values.
Syntax
Return type
MAX() Function Example
What is MAX() in SQL?
The MAX()
function in SQL returns the largest value from a specified column or expression, ignoring NULL values. It is commonly used to find the highest salary, most expensive product, latest date, or largest numeric value in a dataset.
This function is supported in SQL Server, MySQL, PostgreSQL, and Oracle.
The MAX()
function is widely used in data analysis, reporting, and performance tracking.
Parameters:
expression: Column name or expression to evaluate
Example Use Cases:
-- Find highest product price
-- Get most recent order date
-- Find maximum rating per product
Notes:
Behavior: Ignores NULL values in comparison
Performance Considerations: Benefits from indexes
Version Info: Core aggregation function available in all major DBMS
Deprecated/Recommended Alternatives: None
Error Handling:
Error: Returns NULL if all values are NULL
Recommendation: Use COALESCE or IF NULL for NULL handling
Supported Databases:
DBMS
Function / Syntax
Example
Behavior with NULL
MySQL
MAX(expression)
MAX(salary)
Ignores NULL
PostgreSQL
MAX(expression)
MAX(salary)
Ignores NULL
SQL Server
MAX(expression)
MAX(salary)
Ignores NULL
SQLite
MAX(expression)
MAX(salary)
Ignores NULL
BigQuery
MAX(expression)
MAX(salary)
Ignores NULL
Snowflake
MAX(expression)
MAX(salary)
Ignores NULL
Athena
MAX(expression)
MAX(salary)
Ignores NULL
Related Functions:
MIN()
Returns the smallest value from a specified column or expression, ignoring NULL values.
AVG()
Calculates the arithmetic mean (average) of all non-null values in a specified column or expression.
COUNT()
Counts the number of rows or non-null values in a specified column. COUNT(*) counts all rows including nulls, while COUNT(column) counts non-null values in the specified column.
SUM()
Calculates the total sum of all values in a specified column or expression, ignoring NULL values.