Summary: in this tutorial, you will learn how to use the MySQL MAX()
function to get the maximum value in a set of values.
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()
.
We’ll use the payments
table in the sample database to demonstrate the MAX()
function.
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)
In this example, the MAX()
function checks all values in the amount column of the payments table to find the largest amount.
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:
WHERE
clause to get only payments whose year is 2004. We used the YEAR()
function to extract the year from the payment date.MAX()
function in the SELECT
clause to find the largest amount of payments in 2004.This picture shows the output:
3) Using MAX() function in subquery exampleTo 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)
How it works.
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)
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.
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)
In this example:
GROUP BY
clause group payments into groups by customer number.MAX()
function returns the largest payment in each group.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)
If you want to see the names of customers instead of numbers, you can join the payments
table with the customers
table:
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:
SummaryMAX()
function to find the maximum value in a set of values.Was this tutorial helpful?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4