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.
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
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:
The Max MySQL query:
select max(emp_sal_paid) as Highest_salary_paid from tbl_emp_salary_paid
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:
select emp_name,max(emp_sal_paid) as max_paid from tbl_emp_salary_paid
group by emp_name
You can see each employee’s maximum salary in the graphic.