Aggregate function MAX() in SQL

The MAX() function in SQL is an aggregate function that finds the maximum value in a column. This function is essential for Data Analysis, as it helps in identifying the highest value across a dataset, whether that’s the highest sale in a day, the top performing marketing campaign, etc.

Syntax

The syntax for the MAX() function is straightforward. You specify the function and the column you wish to find the maximum value for.

For example, to find the highest priced product, your query would be:

SELECT MAX(price)
FROM products

Using MAX() in detailed analysis

Typically, the MAX() function is utilized alongside GROUP BY statement. It allows us to identify maximum values within specific groups.

SELECT
  country,
  MAX(age)
FROM users
GROUP BY country

This query reports the highest age in each user country.

The most recent record

The MAX() function comes in very handy for dataset exploration. To find the latest activity date in a dataset, MAX() can be applied to a timestamp column.

For example, here’s a query that reports the timestamp of the latest purchase:

SELECT MAX(created_at)
FROM purchases

The MAX() aggregate function is critical in many scenarios: highlight top performers, peak sales, and other maximum values critical to business and research.

Database compatibility

MySQL
PostgreSQL
SQLite
Redshift
Big Query
Snowflake
MAX
MAX
MySQL
PostgreSQL
SQLite
Redshift
Big Query
Snowflake

Find a problem on this page?

Want to get more involved? SQL MDN Docs is an open-source project, check out contribution guidelines.
This page was last modified on April 07, 2024.