×

MySQL GREATEST function

This statement displays the greatest values of the table. The GREATEST function returns a null value when the table contains a null value. The GREATEST function needs a minimum of two columns of the table. If the character value is available in the column, then the function returns the first descending value of the alphabet.

Syntax

The GREATEST function syntax shows below.

GREATEST (data1, data2, data3, dataN);

MySQL GREATEST function with table syntax shows below.

SELECT GREATEST (Column1, Column2, Column3, ColumnN) 
FROM table name WHERE condition;

Prerequisite for GREATEST comparison function

  • Create a new table in the database. We can use the below statement to do this:

CREATE TABLE `world`.`crafts` (

  `nos` INT NOT NULL,

  `items` VARCHAR(45) NULL,

  `quantity` INT NULL,

  `available` INT NULL,

  PRIMARY KEY (`nos`));

  • Insert value in the given table as per requirement.
  • MySQL output command shows table format and its information.
 mysql> select * from crafts;
MySQL GREATEST function

Important points of the GREATEST function

  • The GREATEST function compares the multiple column data.
  • This function is used to find the largest values of the numerical table data.
  • This function returns the last character of the alphabet as the largest value.
  • If a null value is available in the column, then this function returns null data.

Example of the GREATEST function

Example1: Execute the below query to know about the "GREATEST" statement

mysql> SELECT GREATEST (25, 28, 60, 45, 39);

Output

This query returns the below result where the largest value is 60.

MySQL GREATEST function

Example2: It is another example where the function finds the largest value multiple times.

mysql> SELECT GREATEST (0, 1, 2, 0, 1, 2);

Output

This query returns the below result where the largest value is 2 because it removes duplicates and displays only the single largest value.

MySQL GREATEST function

Example3: the GREATEST function example with NULL value.

mysql> SELECT GREATEST (25, 28, 60, 45, 39, NULL);

Output

It returns the below output as the function returns null if it contains a NULL value.

MySQL GREATEST function

Example4: The below example will explain this function with the character values.

mysql> SELECT GREATEST ("good", "bad", "wow", "perfect", "simple", "better");

Output

It returns the result “wow” because it is the largest alphabetical value in specified values.

MySQL GREATEST function

Example5: Execute the below query to know how the "GREATEST" statement works with the WHERE clause. Here WHERE clause applies on the "nos" column.

mysql> select greatest(nos, quantity, available) AS greatest from crafts where nos < 3;

Output

Executing the statement will show the below output that shows two rows and one column. The image displays the greatest value from the three columns:

MySQL GREATEST function

Example7: Execute the below query to know about the "GREATEST" statement with the ORDER BY clause.

mysql> select greatest(nos, quantity, available) AS greatest from crafts order by greatest ASC;

Output

It gives the below output:

MySQL GREATEST function

The image displays the greatest value from the three columns. Here, the numerical value displays in the output table. The output table shows two rows and one column. Here, table data show in ascending order.


Related Topics

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 SUM function

The SUM() function displays the total addition of the table values. It supports the arithmetic operation of the column values. This function does not return a null value. Here, the...

5 minutes read.

MySQL DELETE JOIN

Sometimes join becomes complicated and unwanted. In such cases, we can delete the unwanted joins in the tables. You can delete inner join, left join, right join as per requirement....

3 minutes read.

MySQL NOW() function

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

2 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 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.

MySQL Table

Introduction MySQL table is an essential part of the system. MySQL table stores data using index, rows, and columns. It accesses data from the server quickly because of the table—this table...

22 minutes read.

MySQL RIGHT JOIN

MySQL right join links two tables with each other. The left table column connects with the complete right side table. Each row of the right table tries to connect with...

4 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 Join

The relational database system needs to interconnect multiple tables with each other. MySQL is a popular and easy data management system to connect multiple tables. The foreign key is used...

5 minutes read.

MySQL MIN function

The MIN() function determines the minimum or lowest value of the data set. This function works on numerical data type values. If the table displays zero value, then the row...

4 minutes read.

MySQL CEIL() function

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

3 minutes read.

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

4 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.

Clustered vs Non-Clustered Index

Clustered vs. Non-Clustered Index The difference between clustered and non-clustered index is the most famous question in the database-related interviews. Both indexes have the same physical structure and are stored as...

2 minutes read.

MySQL Constraints

MySQL Constraints Introduction The constraints help to restrict what values should be stored in a table. The constraints provide limitations of the columns or data. This function helps to insert data in...

20 minutes read.

MySQL EXPORT_SET() function

In this context, we will learn how we can use the MySQL EXPORT_SET() function with proper syntax and good examples. Introduction of MySQL EXPORT_SET() function This function returns a string and shows...

3 minutes read.

MySQL Tutorial

In this tutorial, you will get information about the MySQL management system. This tutorial will cover the basic and advanced level MySQL concepts with examples that will help you become...

5 minutes read.

MySQL Table Query

MySQL table query A database stored a lot of data and divided them into different relations known as tables. Each database can contain more than one table. These tables are created...

7 minutes read.

MySQL String Function

The string function is used to maintain and operate string values. This function modifies the string data as per function. We can do the concatenation, conversion, removal and replacing the...

4 minutes read.