AggregateSQL standard

MAX

Returns the largest value among the rows.

Syntax

MAX(<expression>)

An aggregate function: it returns the largest value from a group of rows. It works not only with numbers but also with dates (the latest) and text (the last alphabetically). It skips NULL. To find the smallest value, use MIN.

Data for the examples

employees

namedepartmentsalarybonusphone
Emmabackend1200300+44 7700 900123
Liambackend1000NULLNULL
Oliviafrontend1350150+44 7700 900456
Noahfrontend950NULL+44 7700 900789
Sophiadesign1200200NULL

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

The highest salary
SELECT MAX(salary) AS highest
FROM employees;
highest
1350
The latest order
SELECT MAX(created_at) AS last_order
FROM orders;
last_order
2026-09-02 11:00:00
The highest salary in each department
SELECT department,
MAX(salary) AS highest
FROM employees
GROUP BY department
ORDER BY department;
departmenthighest
backend1200
design1200
frontend1350

Watch out

MAX gives a value, not a row. SELECT name, MAX(salary) FROM employees is an error, because name isn't in GROUP BY. Who exactly earns the most is shown by ORDER BY salary DESC LIMIT 1 or a subquery.

Numbers stored as text are compared as text: of '9' and '10', the larger is '9'.

Don't confuse it with GREATEST: that one compares several values within one row.

Related functions

Learn more in the course