×

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 MONTHNAME() function

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

3 minutes read.

MySQL Queries

MySQL Queries MySQL supports SQL queries in the MySQL interface. These queries help to interact data with the application. MySQL uses create database, user database, create a table, truncate table, and...

13 minutes read.

MySQL RAND() function

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

3 minutes read.

MySQL Advance table Query

MySQL Advance table Query The table is created by index, rows, and columns in the MySQL database. The user saves their information in matrix format. The database requires a query to...

14 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 Unique index

It helps to maintain data integrity to enforce the uniqueness of values in one or more columns. We can create more than one UNIQUE index in a single table, which...

3 minutes read.

MySQL Index

An index is a widely used method to access table information quickly. MySQL requires an index for operating table data stored in rows and columns. It's an entry point of...

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.

MySQL SELF JOIN

This join links table with itself. The inner join, a self join, a right join, and a cross join are connected with two or more two tables. But, the "self...

4 minutes read.

MySQL REVERSE() function

In this context, we will learn how we can use the MySQL REVERSE() function with proper syntax and good examples. Introduction of MySQL REVERSE() function This function could be used to reverse...

2 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 CONCAT() function

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

3 minutes read.

MySQL Full text index

Full text index The full-text index in the table isused to search the full text of the data. This index assigns to the table using a "FULLTEXT" keyword. First, the table...

3 minutes read.

MySQL FROM_BASE64() function

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

2 minutes read.

MySQL TAN() Function

Basically, the TAN() function in MySQL is used to give back the tangent of a mentioned number.In any kind of right-angle triangle, the tangent of an angle is the length...

2 minutes read.

MySQL Date and Time function

The date function displays day, year, month, time, and current date. It shows the date and time as per the requirement of the applications. The data either store on the...

8 minutes read.

MySQL ELT() function

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

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 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 View

Introduction MySQL View is a virtual table to create a clone of the base table. The View does not contain its values or data. MySQL View creates to connect more than...

12 minutes read.