Open In App

MySQL MAX() Function

Last Updated : 19 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In database management, efficient data retrieval is important for making informed decisions. The MySQL MAX() function stands as a useful function for extracting the highest value from a set of records, offering significant benefits for data analysis and reporting.

Whether you're identifying peak sales, the latest dates, or the maximum scores, understanding and using the MAX() function can streamline your data queries and enhance the precision of your insights. This article explores the depth of the MAX() function, providing you with a comprehensive guide to the function.

MySQL MAX() Function

The MySQL MAX() Function mainly returns the maximum value from the specified column in the table. This function is mostly used to find the highest value among the set of multiple values, mostly in the numerical columns.

The MySQL MAX() Function is useful in queries where we need to determine the maximum value in the dataset of columns.

Syntax:

MySQL MAX() function syntax is:

MAX(expression)

Parameters:

  • expression – It is used to specify the expression.

Supported Versions of MySQL

The MySQL MAX function is supported on the following versions:

  • MySQL 5.7
  • MySQL 5.6
  • MySQL 5.5
  • MySQL 5.1
  • MySQL 5.0
  • MySQL 4.1
  • MySQL 4.0
  • MySQL 3.23

MySQL MAX( ) Function Example

Let’s look at some examples of the MAX() function in MySQL. Learning the MAX() function with examples will help in understanding the concept better.

First let’s create a table:

Demo MySQL Database

CREATE TABLE employees(id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10, 2),
hire_date DATE);

INSERT INTO employees(name, salary, hire_date)
VALUES('Gaurav', 60000, '2022-01-15'),
('Yuvraj', 50000, '2021-05-30'),
('Prakash', 85000, '2023-06-10'),
('Shruti', 89000, '2019-11-25');

Example 1: Returning the Maximum Salary

In this example, we are returing the highest salary from the 'salary' column in the 'employees' table.

SELECT MAX(salary) AS max_salary
FROM employees;

Output:

+------------+
| max_salary |
+------------+
| 89000.00 |
+------------+

Example 2: Returning the Recent Hire Date

In this example, we are returning the most recent hire date from the 'hire_date' column in the 'employees' table as 'latest_hire'.

SELECT MAX(hire_date) AS latest_hire
FROM employees;

Output:

+-------------+
| latest_hire |
+-------------+
| 2023-06-10 |
+-------------+

Example 3: Returning Maximum Salary for Employee Hired After 2021

In this example, we are returning the highest salary from the salary column for 'employees' hired after January 1, 2021, as 'max_salary'.

SELECT MAX(salary) AS max_salary
FROM employees
WHERE hire_date > '2021-01-01';

Output:

+------------+
| max_salary |
+------------+
| 85000.00 |
+------------+

Conclusion

In conclusion, the MySQL MAX() function is a valuable SQL function for identifying the highest values within your data sets. Its simplicity and efficiency make it important for data analysis and reporting. By understanding and using this function, you can improve the accuracy and relevance of your database queries, enabling better decision-making and insights.


Next Article
Article Tags :

Similar Reads