MySQL max function

The max function of MySQL

The MySQL Max function is used to return maximum value in the specified column of a table. The function can be used simply with the select statement. You can also use Max function with the group by clause.

See Max function example

In the later part of this tutorial, you can see examples of using Max function in a simple select statement as well as with the group by clause.

The Max function syntax

This is how you can use Max function:

Select max(column_name) from table_name

Where col_name = value

Max examples

To explain the Max function with examples, we have created a table tbl_emp_salary_paid. This table stores salaries of employees with their names.

Max example in simple select query

The following query will return the highest salary in the tbl_emp_salary_paid table. You can see the table before and after using the max function by clicking the links below:

See table data before Max function

The Max MySQL query:


See table data after Max

You can also use MySQL Max on date based columns.

Max with Group by example

In above example, we returned single maximum value in the whole table. As our table stores salaries of employees with their names, what if we need to know the highest salary paid to each employee? This is how you can do it by using the group by clause with max function:

See this example online with graphic

The query:


You can see each employee’s maximum salary in the graphic.

 

Also see: MySQL count | MySQL sum

Was this article helpful?

Related Articles

Leave A Comment?