How to Use the MAX() Function in MySQL

08/04/2021

Contents

In this article, you will learn how to use the MAX() function in MySQL.

Using the MAX() function in MySQL

The MAX() function in MySQL is used to find the maximum value in a given column of a table.

Syntax

The syntax for using the MAX() function in MySQL is as follows:

SELECT MAX(column_name) FROM table_name;

Examples

Suppose we have a table named “employees” with columns “employee_id” and “salary”. We can use the MAX() function to find the maximum salary in the table as follows:

SELECT MAX(salary) FROM employees;

This query will return the maximum value of the “salary” column in the “employees” table.

If we want to find the employee with the highest salary, we can use the following query:

SELECT employee_id, salary FROM employees WHERE salary = (SELECT MAX(salary) FROM employees);

This query will return the employee ID and salary of the employee with the highest salary in the “employees” table.

We can also use the MAX() function with multiple columns to find the maximum value of multiple columns. For example:

SELECT MAX(column1), MAX(column2) FROM table_name;

This query will return the maximum value of “column1” and “column2” in the “table_name” table.