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