×

MySQL MAX function

The MAX function is a type of aggregation function that determines the maximum value of the table data. This function works on numerical data type values. If the table displays zero value, then the row does not exist in the table column. The max function returns a maximum value of the single or multiple columns.

Syntax

The basic MAX () function syntax shows below.

MAX(function_expression)

MySQL MAX () function syntax with table shows below.

SELECT MAX(function_expression) FROM Table_Name WHERE condition;

Description

  • The MAX() function returns a maximum number of the rows of the table.
  • The "function_expression" represents the table value or column data for the "SUM" function's operation.
  • The "WHERE" clause applies logical conditions on the MAX() function.

Prerequisite for MAX function

  • Create the new database in the command line prompt interface.
mysql> create database mysqldb;
  • Use the given database to create and operate tables.
mysql> use mysqldb;
  • Create a new table with the columns and their data type.
mysql> create table product (  

    number int NOT NULL,

    product varchar(100) NOT NULL,   

    available int, 

    required int, 

    received_date date

    ); 
  • Insert the data in the table as per data type.
mysql> insert into product(number, product, available, required, received_date) 

VALUES   

(1, 'inkpen', 78, 45, '2021-07-01'), 

(2, 'ballpen', 100, 65, '2021-07-01'), 

(3, 'pencil', 78, 45, '2021-08-24'), 

(4, 'eraser', 50, 50, '2021-11-21'), 

(5, 'pen holder', 90, 50, '2021-07-01'), 

(6, 'books', 100, 65, '2021-10-13');
  • You can refer to the table structure and information using the output query.
mysql> SELECT * FROM product;
MySQL MAX() function

Examples of the MAX function

1) Example: The following query shows the operation of the MAX function. This query is a basic sample of the "MAX" function. This function works on the numerical data type.

mysql> SELECT MAX(available) AS available_product  FROM product;

 Output

MySQL MAX() function

The above output shows you the maximum value of the "available" column. This output returns the largest value of the given data.

2) Example: The below example explain the MAX function with the date value. Execute the MAX function on the date column. This function converts a date into a number and operates to determine the largest value.

mysql> SELECT MAX(received_date ) AS date FROM product;

Output

MySQL MAX() function

The output returns the largest date of the product table. The MAX function displays the largest date in date format.

3) Example: The MAX function with the "DISTINCT" and the "WHERE" clauses example.

Execute the below query in which the MAX function works with the WHERE clause. This condition applies to the "available" column.

mysql> SELECT MAX(DISTINCT available) AS available_product  
FROM product 
WHERE available < 60;

 Output

MySQL MAX() function

4) Example: The MAX functions with multiple columns example and output.

This example works similarly to the single-column MAX function. Here, you can use multiple columns like a number, available, required, and date column of the table.

mysql> SELECT MAX(number) AS number, 
MAX(received_date) AS date, 
MAX(available) AS available_product, 
MAX(required) AS required_product
FROM product;

Output

MySQL MAX() function

The max function returns the maximum value of each column in the above image. This image shows you the largest value of the given table. The multiple rows data displays in a single row in the output table.

5) Example: The MAX functions with the "WHERE" clause and "AND" operator example.

The MAX function comes with a number, available, and receives columns of the table. This clause works on the "available" and "required" columns of the table. The AND logical condition comes between the "available" and "required" columns.

mysql> SELECT MAX(number) AS number, 
MAX(received_date) AS date, 
MAX(available) AS available_product, 
MAX(required) AS required_product
FROM product 
WHERE number < 4 AND required > 50;

Output

MySQL MAX() function

6) Example: The MAX functions with the "GROUP BY" clause example.

This MAX function example uses multiple columns of the table along with the GROUP BY clause. This grouping clause works on the available column of the table.

mysql> SELECT MAX(number) AS number, 
MAX(received_date) AS date, 
MAX(available) AS available_product, 
MAX(required) AS required_product
FROM product
GROUP BY available;

Output

MySQL MAX() function

This function displays the largest values of the table. This output returns four rows of the table data. The "GROUP BY" clause applies to the available column.

7) Example: The MAX functions with the "GROUP BY" and "HAVING" clauses.

Execute the MAX function query using multiple columns of the table. This function uses the ORDER BY clause with ascending order condition.

mysql> SELECT MAX(number) AS number, 
MAX(received_date) AS date, 
MAX(available) AS available_product, 
MAX(required) AS required_product
FROM product
GROUP BY available
HAVING available < 60;

Output

MySQL MAX() function

The MAX function returns the largest value of the name, date, available_product, and required_product columns. The "HAVING" clause applies less than 50 value conditions on the table.


Related Topics

MySQL: Entity-Relationship Model

Entity-Relationship Model Entity-Relationship model or E R model is used to create a relationship between different attributes or entities. It describes the structure of the database with the help of the...

5 minutes read.

MySQL Math Function

The math function operates numerical values and displays required number data. The math function finds out sin, cos, tan values. Here, you find out the absolute value, reminder, and logarithmic...

6 minutes read.

MySQL DATE_ADD() function

In this context, we will learn how we can use the MySQL DATE_ADD() function with proper syntax and good examples. Introduction of MySQL DATE_ADD() function In MySQL, the DATE_ADD() function is used...

3 minutes read.

MySQL UPDATE JOIN

The UPDATE JOIN is a MySQL statement used to perform cross-table updates that means we can update one table using another table with the JOIN clause condition. The "update join"...

5 minutes read.

MySQL SOUNDEX() function

In this context, we will learn how we can use the MySQL SOUNDEX() function with proper syntax and good examples. Introduction of MySQL SOUNDEX() function SOUNDEX() function in MySQL is used to...

3 minutes read.

MySQL LOCATE() function

In this context, we will learn how we can use the MySQL LOCATE() function with proper syntax and good examples. Introduction of MySQL LOCATE() function LOCATE() function in MySQL is used for...

3 minutes read.

MySQL Create index

An index creation helps to make a row of the table unique. The index operates and handles table data quickly. MySQL index requires NOT NULL column constraint. The index column...

2 minutes read.

MySQL RIGHT() Function

In this context, we will learn how we can use the MySQL RIGHT() Function with proper syntax and good examples. Introduction of MySQL RIGHT() function The RIGHT() function in MySQL is used...

3 minutes read.

MySQL Environmental Setup

The MySQL Environmental Setup MySQL is free, open-source, and cross-platform software, which can be downloaded from its official website. MySQL management system installs on Linux, macOS, and windows. MySQL requires a framework...

6 minutes read.

MySQL PERIOD_DIFF() function

In this context, we will learn how we can use the MySQL PERIOD_DIFF() function with proper syntax and good examples. Introduction of MySQL PERIOD_DIFF() function This function in MySQL is used to...

2 minutes read.

MySQL LIKE() function

In this context, we will learn how we can use the MySQL LIKE() function with proper syntax and good examples. Introduction of MySQL LIKE() function The LIKE function is a logical function...

4 minutes read.

MySQL Composite Index

Composite Index A composite index is an index that is used on multiple columns. MySQL management system manages multiple columns simultaneously. Therefore, the single index contains multiple columns in one query...

3 minutes read.

MySQL Control flow function

The control flow function uses values or operands for logical operations. These functions can works on single and multiple conditions. The control flow function is based on the Boolean expression....

3 minutes read.

MySQL NATURAL JOIN

The natural join combines multiple tables using a column with the same name and data type. This operation combines a row of the two tables using common columns. This join...

4 minutes read.

MySQL HEX() function

In this context, we will learn how we can use the MySQL HEX() function with proper syntax and good examples. Introduction of MySQL HEX() function For returning an equivalent hexadecimal string value...

3 minutes read.

MySQL Clauses

MySQL Clauses MySQL system clauses are keywords or statements to handle information. It helps to operate a group of the data and apply it to require conditions. The clauses apply conditions...

16 minutes read.

MySQL DATE_FORMAT() function

In this context, we will learn how we can use the MySQL DATE_FORMAT() function with proper syntax and good examples. Introduction of MySQL DATE_FORMAT() function DATE_FORMAT() function in MySQL is used to...

3 minutes read.

MySQL RTRIM() function

In this context, we will learn how we can use the MySQL RTRIM() function with proper syntax and good examples. Introduction of MySQL RTRIM() function It is the function in MySQL that...

2 minutes read.

MySQL Invisible Index

Invisible Index MySQL index decides an option to the availability of the index for the query optimizer. This index shows the visible and invisible options to displays the index column in...

3 minutes read.

MySQL INSTR() Function

In this context, we will learn how we can use the MySQL INSTR() function with proper syntax and good examples. Introduction of MySQL INSTR() function This function in MySQL returns the location...

4 minutes read.