Skip to content
Laviyx Learn

Try

SQL course menu

SQL MAX(): Find the Largest Value (Numbers, Dates and Text)

Learn the SQL MAX function: the highest number, latest date, or last text value, MAX per group, the range between MIN and MAX, and how to find the row that holds the maximum.

Beginner5 min readUpdated
On this page

MAX returns the largest value in a column: the highest salary, the most recent order, the most expensive product. It works exactly like MIN, in the opposite direction.

The example tables#

employees

namedepartmentsalary
AshaSales52000
RaviEngineering78000
MeeraEngineering81000
JohnMarketing46000
PriyaSales61000

orders

order_idcustomer_idamountorder_date
10112502026-01-05
10211202026-02-11
10325602026-02-20
1045902026-03-02

Highest number and latest date#

SQLRun
SELECT MAX(salary) AS highest
FROM employees;
Output
highest
-------
81000
SQLRun
SELECT MAX(order_date) AS last_order
FROM orders;
Output
last_order
----------
2026-03-02

For text, MAX returns the value that sorts last alphabetically.

MAX per group#

Each customer's biggest order:

SQLRun
SELECT customer_id, MAX(amount) AS biggest_order
FROM orders
GROUP BY customer_id
ORDER BY customer_id;
Output
customer_id | biggest_order
------------+--------------
1           | 250
2           | 560
5           | 90

MIN and MAX together#

Aggregates can be combined in one query and in calculations. The spread between the highest and lowest salary:

SQLRun
SELECT MIN(salary) AS lowest,
       MAX(salary) AS highest,
       MAX(salary) - MIN(salary) AS salary_range
FROM employees;
Output
lowest | highest | salary_range
-------+---------+-------------
46000  | 81000   | 35000

Who has the maximum?#

As with MIN, find the value first and then the rows that match:

SQLRun
SELECT name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
Output
name  | salary
------+-------
Meera | 81000

"The highest-paid person in each department" is a harder version of this question. It needs a correlated subquery or a window function, which you will learn in ROW_NUMBER and RANK.

NULL values#

MAX ignores NULLs and returns NULL when there is nothing to compare.

Common mistakes#

Practice#

Practice exercise

Using products (name, category, price), return the most expensive price in each category, highest first.

Show hint

MAX(price) with GROUP BY category, ordered descending.

Show solution
SQLRun
SELECT category, MAX(price) AS most_expensive
FROM products
GROUP BY category
ORDER BY most_expensive DESC;
Output
category    | most_expensive
------------+---------------
Electronics | 55000
Furniture   | 7000
Stationery  | 60

Key takeaway#

Finished this lesson?