MySQL MAX() Function

Summary: in this tutorial, you will learn how to use the MySQL MAX() function to get the maximum value in a set of values.

Introduction to MySQL MAX() function

In MySQL, the MAX() function returns the maximum value in a set of values.

The MAX() function comes in handy in many cases such as finding the highest number, the most expensive product, or the largest payment from customers.

Here’s the syntax of the MAX() function :

MAX(DISTINCT expression)Code language: SQL (Structured Query Language) (sql)

In this syntax:

  • expression: is a column or an expression that you want to find the maximum value.

If you use the DISTINCT operator, the MAX() function returns the maximum value of distinct values, which is the same as the maximum value of all values. It means that DISTINCT does not have any effects on the MAX() function.

Notice that DISTINCT has effects on other aggregate functions such as COUNT(), SUM(), and AVG().

MySQL MAX() function examples

We’ll use the payments table in the sample database to demonstrate the MAX() function.

1) Using MySQL MAX() function to find the maximum value in a column example

This example uses the MAX() function to return the largest amount of all payments:

SELECT MAX(amount)
FROM payments;Code language: SQL (Structured Query Language) (sql)

Try It Out

MySQL MAX function example

In this example, the MAX() function checks all values in the amount column of the payments table to find the largest amount.

2) Using MySQL MAX() function with WHERE clause

The following statement uses the MAX() function to find the largest payment in 2004:

SELECT 
    MAX(amount) largest_payment_2004
FROM
    payments
WHERE
    YEAR(paymentDate) = 2004;Code language: SQL (Structured Query Language) (sql)

In this example:

  • First, use a condition in the WHERE clause to get only payments whose year is 2004. We used the YEAR() function to extract the year from the payment date.
  • Then, use the MAX() function in the SELECT clause to find the largest amount of payments in 2004.

This picture shows the output:

mysql max with where clause

3) Using MAX() function in subquery example

To obtain not only the largest payment amount but also additional payment information, such as customer number, check number, and payment date, you can utilize the MAX() function within a subquery, as demonstrated in the following query:

SELECT 
    *
FROM
    payments
WHERE
    amount = (SELECT 
            MAX(amount)
        FROM
            payments);Code language: SQL (Structured Query Language) (sql)

Try It Out

How it works.

  • The subquery returns the largest amount of all payments.
  • The outer query gets the payment whose amount is equal to the largest amount returned from the subquery and also other payment information.

Another way to do this without using the MAX() function is to sort the result set in descending order using the ORDER BY clause and get the first row in the result set using the LIMIT clause as follows:

SELECT 
    *
FROM
    payments
ORDER BY amount DESC
LIMIT 1;Code language: SQL (Structured Query Language) (sql)

Try It Out

If you don’t have an index on the amount column, the second query with the LIMIT clause is faster because it examines all rows in the payments table, while the first query examines all the rows in the payments table twice, first once in the subquery and another in the outer query.

However, if the amount column is indexed, the first query executes faster.

4) Using MySQL MAX() with GROUP BY clause example

To find the maximum value for every group, you use the MAX function with the GROUP BY clause.

This statement uses the MAX() to get the largest payment from each customer:

SELECT 
    customerNumber, MAX(amount)
FROM
    payments
GROUP BY customerNumber
ORDER BY MAX(amount);Code language: SQL (Structured Query Language) (sql)

Try It Out

mysql max with group by clause

In this example:

  • First, the GROUP BY clause group payments into groups by customer number.
  • Second, the MAX() function returns the largest payment in each group.

5) Using MySQL MAX() with HAVING clause

When you use the MAX() function with the GROUP BY clause, you can find the maximum value for each group.

If you want to filter groups based on a condition, you can use the MAX() function in a HAVING clause.

The following query finds the largest payment of each customer; and based on the returned payments, gets only payments whose amounts are greater than 80,000 .

SELECT 
    customerNumber, MAX(amount)
FROM
    payments
GROUP BY customerNumber
HAVING MAX(amount) > 80000
ORDER BY MAX(amount);Code language: SQL (Structured Query Language) (sql)

Try It Out

mysql max with having clause example

If you want to see the names of customers instead of numbers, you can join the payments table with the customers table:

customers payments
SELECT 
    customerName, 
    MAX(amount)
FROM
    payments
INNER JOIN customers USING (customerNumber)    
GROUP BY 
    customerNumber
HAVING 
    MAX(amount) > 80000
ORDER BY 
    MAX(amount);Code language: SQL (Structured Query Language) (sql)

Here is the output:

mysql max with inner join

Summary

  • Use the MAX() function to find the maximum value in a set of values.
Was this tutorial helpful?