MySQL Tutorial

MySQL Tutorial MySQL Features MySQL Database Introduction MySQL Environmental Setup MySQL Data Types MySQL variable MySQL Advance table Query MySQL database queries MySQL Entity-Relationship Model MySQL Table Query MySQL Operators MySQL logical conditions MySQL Queries MySQL Clauses Clustered vs Non-Clustered Index MySQL Full text index MySQL Descending Index MySQL Invisible Index MySQL Composite Index MySQL Prefix index MySQL Index MySQL Create index MySQL Drop Index MySQL Show index MySQL Unique index MySQL Table MySQL Variable MySQL View MySQL Constraints MySQL Command Line Client Basic Queries MySQL Stored Procedure MySQL IF Statement MySQL Subquery MySQL Triggers

MySQL Join

MySQL Join MySQL CROSS JOIN MySQL DELETE JOIN MySQL EQUI JOIN MySQL INNER JOIN MySQL Union MySQL NATURAL JOIN MySQL RIGHT JOIN MySQL SELF JOIN MySQL UPDATE JOIN

MySQL Function

MySQL Function MySQL AVG() Function MySQL SUM() Function MySQL String() Function MySQL Advance() Function MySQL Aggregate() Function MySQL COALESCE() Function MySQL Control Flow Function MySQL COUNT() Function MySQL Date And Time Function MySQL GREATEST() Function MySQL ISNULL() Function MySQL LEAST() Function MySQL Math() Function MySQL MAX() Function MySQL MIN() Function MySQL find_in_set() function MySQL ASIN() Function MySQL CEIL() function MySQL CEILING() function MySQL TAN() Function MySQL Truncate() Function MySQL FLOOR() function MySQL LN() function MySQL LOG2() function MySQL LOG10() function MySQL MOD() function MySQL PI() function MySQL POW() function MySQL RADIANS() function MySQL RAND() function MySQL ROUND() function MySQL Character Length Function MySQL Current Date Function MySQL Date Add Function MySQL Date Format Function MySQL Datediff Function MySQL Day Function MySQL Elt Function MySQL Export Set Function MySQL Field Function MySQL Format Function MySQL From Base64 Function MySQL Hex Function MySQL Insert Function MySQL Instr Function MySQL Length Function MySQL CONCAT() function MySQL FIND_IN_SET() function MySQL LIKE() function MySQL LOAD_FILE() function MySQL LOCATE() function MySQL LOG() function MySQL MONTHNAME() function MySQL NOW() function MySQL PERIOD_ADD() function MySQL PERIOD_DIFF() function MySQL POWER() function MySQL QUARTER() function MySQL REVERSE() function MySQL RIGHT() Function MySQL RPAD() function MySQL RTRIM() function MySQL SEC_TO_TIME() function MySQL SOUNDEX() function

Questions

Which Clause is Similar to Having Clause in MySQL

Misc

MySQL Error 1046 - No Database Selected Failed to Start MySQL Service Unit MySQL Service Unit not Found Import MySQL Connector Mudule not Found Error No Module Named MySQL Joins Available in MySQL MySQL Docs MySQL Download For Windows 7 64 Bit MySQL Error Code 1064 MySQL Export MySQL History MySQL Host MySQL Import MySQL Drop All Tables MySQL Drop MySQL Error Code 1175 MySQL Events MySQL Except MYSQL Foreign Key Constraint MySQL If Exists MySQL IndexOf MySQL List All Tables json_extract in MySQL TIMESTAMPDIFF in MySQL MySQL Syntax Checker Sudo MySQL Secure Installation

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.