×

MySQL LEAST function

This statement displays the smallest value of the table. The LEAST function returns a null value when the table contains a null value. If the table contains all numerical values, then the function returns the lowest value of the table data.

Syntax

The LEAST function syntax shows below.

LEAST (data1, data2, data3, dataN);

MySQL GREATEST function with the table syntax shows below.

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

Prerequisite for LEAST comparison function

  • Create a new table in the MySQL database.

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 table.
  • MySQL output command shows table format and its information.
•  mysql> select * from crafts;
MySQL LEAST function

Important points of the LEAST function

  • The LEAST function compares the multiple column data.
  •  This function is used to find out the lowest values of the numerical table data.
  • This function returns the last character of the alphabet as the lowest value.
  • If a null value is found in the column, the LEAST function returns null data.

Example of the LEAST function

Example1: Execute the below query to know about the "LEAST" statement.

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

OUTPUT

MySQL LEAST function

The image shows the output as the "lowest" value. If a single value is available, then the "LEAST" function returns it.

Example2: Execute the below query to know how the "LEAST" statement works when it finds duplicate values.

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

OUTPUT

MySQL LEAST function

The image shows the output as the "lowest" value. This query removes duplicates and displays the single lowest value.

Example3: Execute the below query to see the result when it finds NULL value.

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

OUTPUT

Executing the statement will return the NULL result. It is because the function returns NULL when the specified columns have a NULL value.

MySQL LEAST function

Example4: Execute the below query to see how the "LEAST" statement works with the string data.

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

OUTPUT

MySQL LEAST function

The image shows the output as the "lowest" value. This function returns the first ascending value of the alphabet. Here, the output shows "wow" as the least value.

Example5: The LEAST function with table data example shows below. Here, the crafts table uses three columns to compare the lowest value.

mysql> select least(quantity, available) from crafts;

OUTPUT

MySQL LEAST function

Example6: the LEAST function with WHERE clause example shows below.

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

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

OUTPUT

MySQL LEAST function

The output table shows two rows and one column. The image displays the lowest value from the three columns.


Related Topics

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

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 Truncate() Function

In this context, we will learn how we can use the MYSQL TRUNCATE function to truncate a number to a mentioned number of decimal places. Syntax of MySQL Truncate Function A number...

2 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 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 CROSS JOIN

MySQL CROSS JOIN combines all possibilities of the two or more tables and returns the result that contains every row from all contributing tables. It links several columns from the...

4 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 Drop Index

Sometimes we have an index that is not required in data operations. In that case, we can use this statement to remove an existing index from the table. We can...

4 minutes read.

MySQL PI() function

In this context, we will learn how we can use the MySQL PI() function with proper syntax and good examples. Introduction of MySQL PI() function PI() 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 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: 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 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 logical conditions

MySQL logical conditions Introduction MySQL handles data with clauses, operators, and conditions. The logical condition is used to compare information and returns the required output. This condition applies logic to MySQL expressions...

7 minutes read.

MySQL vs Oracle

What is MySQL? The open-source MySQL relational database management system is a vital software component for web-based applications. As the data is saved and sent over the internet, databases and related...

6 minutes read.

MySQL INSERT() Function

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

2 minutes read.