
424 views
MySQL max()
The MAX() function in MySQL is an aggregate function used to retrieve the maximum (largest) value from a specified column. It is commonly used to find the maximum value in a dataset or to retrieve the latest date in a date column. The basic syntax of the MAX() function is as follows:
MAX(expression)
expression: This should be a column or an expression from which you want to find the maximum value.
Here are some common ways to use the MAX() function:
- Find the Maximum Value in a Column:
To find the maximum value in a column, useMAX(column_name).
SELECT MAX(price) FROM products;
- Find the Maximum Date in a Date Column:
To find the latest date in a date column, useMAX(date_column).
SELECT MAX(order_date) FROM orders;
- Find the Maximum Value Based on a Condition:
You can find the maximum value in a column that meets specific conditions by using theMAX()function with aWHEREclause.
SELECT MAX(score) FROM test_scores WHERE subject = 'Math';
- Find the Maximum Value Grouped by a Column:
When you want to find the maximum value for each group within a dataset, you can use theGROUP BYclause with theMAX()function.
SELECT department, MAX(salary) FROM employees GROUP BY department;
The MAX() function returns the largest value from the specified column. It’s a helpful tool for identifying the maximum value in your data, whether that’s the highest price, the latest date, or the maximum score in a specific category.