×

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

In this context, we will learn how we can use the MySQL POWER() function with proper syntax and good examples. Introduction of MySQL POWER() function The value of a number raised to...

3 minutes read.

MySQL FIELD() function

In this context, we will learn how we can use the MySQL FIELD() function with proper syntax and good examples. Introduction of MySQL FIELD() function The index position of a mentioned value...

3 minutes read.

MySQL Subquery

The subquery is a MySQL query used to manage data operations. Mainly subquery helps to retrieve data with the necessary condition. It creates a nested query with two different queries....

5 minutes read.

MySQL Aggregate function

The aggregation function works on the multiple values and returns single value output.  It makes the advanced and complex operation simple. The summation (SUM), MAX, AVG, MIN, COUNT functions are...

5 minutes read.

MySQL Prefix index

Prefix index This index query creates an index column in the string or character column. MySQL system can create multiple indexes in the table. It will create in a table as...

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

find_in_set() function in MySQL

This Function is used for finding the position of a particular string from the list of strings. For suppose if the specified string is repeated multiple times, then this functions...

4 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 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 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 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 PERIOD_ADD() function

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

2 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 database queries

MySQL database queries Database queries help to create, use, show and, delete the database. A Database is a collection of several tables and data. The database uses SQL language to perform...

3 minutes read.

MySQL FORMAT() function

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

2 minutes read.

MySQL INNER JOIN

MySQL inner join connects two tables by using their common columns. It is the basic join of the MySQL system. It is used to returns only those results from the...

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