Back to Library

MAX() in SQL

Returns the largest value from a specified column or expression, ignoring NULL values.

Syntax

MAX(expression)
Return type
Same as input type (number, date, string, etc.)

MAX() Function Example

SELECT MAX(salary) FROM employees; 
-- Output: 120000.00 (highest salary)

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

SELECT MAX(price) AS highest_price FROM products;
-- Output:
highest_price
-------------
   $999.99

 -- Get most recent order date 

SELECT MAX(order_date) AS latest_order FROM orders;
-- Output:
latest_order
-------------------
2024-03-28 14:45:33

-- Find maximum rating per product 

SELECT product_id, MAX(rating) AS highest_rating FROM reviews GROUP BY product_id;
-- Output:
product_id    product_name      highest_rating
----------    -------------     --------------
P100          Coffee Maker           5.0
P101          Headphones            5.0
P102          Phone Charger         4.8
P103          Laptop Stand          5.0
P104          Mouse Pad             4.9

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:

STDEV()

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

Calculates the standard deviation (population or sample) of a set of numeric values.

VARIANCE()

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

Calculates the statistical variance (population or sample) of a set of numeric values.

STRING_AGG()

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

Concatenates values from multiple rows into a single string, with specified delimiter.

GROUP_CONCAT()

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

Concatenates values from multiple rows into a single string, with optional delimiter and ordering.

MAX()

Returns the largest value from a specified column or expression, ignoring NULL values.

Returns the largest value from a specified column or expression, ignoring NULL values.

Returns the largest value from a specified column or expression, ignoring NULL values.

Returns the largest value from a specified column or expression, ignoring NULL values.

One place for all your queries,
directly on your SQL editor