LEAST() Function in MySQL
Last Updated :
06 Sep, 2024
The LEAST()
function in MySQL is a versatile tool that returns the smallest (minimum) value from a list of expressions. It can be used with numbers, strings, or even columns of data to find the minimum value according to their data type.
In this article, We will learn about LEAST() Function in MySQL in detail with the help of various examples and so on.
LEAST() Function in MySQL
- The
LEAST()
function in MySQL returns the smallest (minimum) value from a list of expressions.
- It compares two or more expressions and returns the one with the lowest value according to their data type.
Syntax:
LEAST(X1, X2, X3, ...)
Parameter:
This method accepts N parameter as mentioned above and described below :
- X1, X2, X3...: The list of values from which smallest to be evaluated.
Returns:
It returns the smallest value.
Examples of LEAST() Function in MySQL
Example 1:
Finding the Smallest number between given numbers using LEAST() function.
SELECT LEAST(10, 20, 30, 40) AS Least_Value;
Output:
+-------------+
| Least_Value |
+-------------+
| 10 |
+-------------+
This query selects the smallest value among the numbers `10`, `20`, `30`, and `40` using the `LEAST()` function and returns it as `Least_Value`. The result will be `10`.
Example 2:
Finding Smallest value between given string using LEAST() function.
SELECT LEAST( 'MySQL', 'MS ACCESS', 'SQL') AS LeastValue_String;
Output:
+-------------------+
| LeastValue_String |
+-------------------+
| MS ACCESS |
+-------------------+
Explanation: This query compares the three strings `'MySQL'`, `'MS ACCESS'`, and `'SQL'` lexicographically and returns the smallest (earliest in alphabetical order) string as `LeastValue_String`. In this case, the result would be `'MS ACCESS'`, since it comes first alphabetically among the three.
Example 3:
The LEAST function can also be used to find the Smallest value of a column data . To demonstrate create a table named.
Student :
CREATE TABLE Student(
Student_id INT AUTO_INCREMENT,
Student_name VARCHAR(100) NOT NULL,
Student_Class VARCHAR(20) NOT NULL,
Subject1 INT NOT NULL,
Subject2 INT NOT NULL,
Subject3 INT NOT NULL,
Subject4 INT NOT NULL,
PRIMARY KEY(Student_id )
);
Now inserting some data to the Student table :
INSERT INTO
Student(Student_name, Student_Class, Subject1, Subject2, Subject3, Subject4)
VALUES
('Sayan', 'X', 81, 90, 86, 98 ),
('Nitin', 'X', 90, 84, 88, 90 ),
('Aniket', 'X', 81, 80, 87, 90 ),
('Abdur', 'X', 85, 90, 80, 90 ),
('Sanjoy', 'X', 88, 82, 84, 90 ) ;
So, Our table looks like :
+------------+--------------+---------------+----------+----------+----------+----------+
| Student_id | Student_name | Student_Class | Subject1 | Subject2 | Subject3 | Subject4 |
+------------+--------------+---------------+----------+----------+----------+----------+
| 1 | Sayan | X | 81 | 90 | 86 | 98 |
| 2 | Nitin | X | 90 | 84 | 88 | 90 |
| 3 | Aniket | X | 81 | 80 | 87 | 90 |
| 4 | Abdur | X | 85 | 90 | 80 | 90 |
| 5 | Sanjoy | X | 88 | 82 | 84 | 90 |
+------------+--------------+---------------+----------+----------+----------+----------+
Now, we are going to find least marks for every student among all subjects.
Select
Student_id, Student_name, LEAST(Subject1, Subject2, Subject3, Subject4) as Least_Mark
FROM Student;
Output:
+------------+--------------+------------+
| Student_id | Student_name | Least_Mark |
+------------+--------------+------------+
| 1 | Sayan | 81 |
| 2 | Nitin | 84 |
| 3 | Aniket | 80 |
| 4 | Abdur | 80 |
| 5 | Sanjoy | 82 |
+------------+--------------+------------+
This query retrieves the student ID and name from the `Student` table, along with the minimum mark among four subjects (`Subject1`, `Subject2`, `Subject3`, and `Subject4`) for each student, and labels it as `Least_Mark`.
Conclusion
The LEAST()
function in MySQL is an efficient and straightforward way to determine the minimum value among multiple expressions or columns. Whether working with numerical data or strings, this function provides a reliable method for quickly finding the smallest value, making it an essential function for data analysis and comparison tasks.
Similar Reads
LEFT() Function in MySQL The LEFT() function in MySQL is used to extract a specified number of characters from the left side of a given string. It uses its second argument to decide, how many characters it should return. Syntax: LEFT (str, len)Parameter: This function accepts two parameters as mentioned above and described
1 min read
LN() Function in MySQL LN() function : It is the function in MySQL is used to calculate the natural logarithm of a specific number with base e . The number must be greater than 0, otherwise it will return NULL. Syntax : LN(X) Parameter : LN() function accepts one parameter as mentioned above in the syntax and described be
2 min read
LOG() Function in MySQL LOG() function in MySQL is used to calculate the natural logarithm of a specific number. The number must be >0 Otherwise it will return NULL. Syntax : LOG(X) Parameter : This method accepts one parameter as mentioned above and described below : X : A number whose logarithm value we want to calcul
3 min read
INSERT() function in MySQL INSERT() : This function in MySQL is used for inserting a string within a string, removing a number of characters from the original string. Syntax : INSERT(str, pos, len, newstr) Parameters : This method accepts four parameter. str - Original string in which we want to insert another string. pos - T
2 min read
PLSQL | LEAST Function The LEAST is an inbuilt function in PLSQL which is used to return the least value from a given list of some expressions. These expressions may be numbers, alphabets etc. Syntax: LEAST(exp1, exp2, ... exp_n) Parameters Used: This function accept some parameters like exp1, exp2, ... exp_n. These each
2 min read
LOG2() Function in MySQL In this article, we are going to cover the LOG2() function that means it will calculate the logarithm of a specific number with base 2. Pre-requisite :LOG function LOG2() function in MySQL is used to calculate the natural logarithm of a specific number with base 2. The number must be >0 Otherwise
2 min read
MySQL MIN() Function The MySQL MIN() function is used to get the smallest value in a number set. Suppose you have a table with a list of different products and their corresponding prices; you would want to know which one has the lowest price. Here, the MIN() function will return that answer to you in the easiest possibl
3 min read
MONTH() function in MySQL MySQL MONTH() function returns the month from the given date. It returns a month value between 1 and 12 or returns 0 when the month part of the date is 0. It's a useful SQL function for date manipulation and analysis, particularly when dealing with reports or queries that require month-based groupin
3 min read
MOD() Function in MySQL The MOD() function in MySQL is used to find the remainder of one number divided by another. The MOD() function returns the remainder of dividend divided by divisor. if the divisor is zero, it returns NULL. Syntax: MOD(N, M) or N % M or N MOD M Parameter : MOD() function accepts two parameter as ment
4 min read
MID() function in MySQL MID() : This function in MySQL is used to extract a substring from a given input string. If the starting position is a positive number, then the substring of the given length will be extracted from the starting index. If negative, then the substring of the given length will be extracted from the end
2 min read