MySQL LEAST() Function



MySQL LEAST() Function

The MySQL LEAST() function returns the smallest value from a list of expressions. This function is essential for finding the minimum value among a set of values in SQL queries.


Syntax

SELECT LEAST(expr1, expr2, ..., exprN) AS result
FROM table_name;

The LEAST() function has the following components:

  • expr1, expr2, ..., exprN: A list of expressions to be evaluated.
  • result: An alias for the resulting value.
  • table_name: The name of the table from which to retrieve the data.

Example MySQL LEAST() Function

Let's look at some examples of the MySQL LEAST() function:

Step 1: Using the Database

USE mydatabase;

This query sets the context to the database named mydatabase.

MySQL USE DATABASE

Step 2: Creating a Table

Create a table to work with:

CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    salary DECIMAL(10, 2),
    bonus DECIMAL(10, 2),
    commission DECIMAL(10, 2)
);

This query creates a table named employees with columns for id, first_name, last_name, salary, bonus, and commission.

MySQL CREATE TABLE

Step 3: Inserting Initial Rows

Insert some initial rows into the table:

INSERT INTO employees (first_name, last_name, salary, bonus, commission)
VALUES ('John', 'Doe', 50000.00, 5000.00, 2000.00),
       ('Jane', 'Smith', 60000.00, 7000.00, 2500.00),
       ('Jim', 'Brown', 55000.00, NULL, 3000.00),
       ('Jake', 'White', 52000.00, 6000.00, 4000.00),
       ('Jill', 'Green', 45000.00, 2000.00, 1000.00);

This query inserts five rows into the employees table.

MySQL INSERT INTO TABLE

Step 4: Using LEAST with WHERE Clause

Use the LEAST function to find the minimum value in a list of expressions:

SELECT first_name, last_name, LEAST(salary, bonus, commission) AS lowest_income
FROM employees;

This query retrieves the first_name and last_name columns from the employees table and returns the minimum value among the salary, bonus, and commission columns as lowest_income.

MySQL LEAST WITH WHERE CLAUSE

Step 5: Using LEAST with Multiple Columns

Use the LEAST function with multiple columns:

SELECT first_name, last_name, LEAST(bonus, commission) AS min_bonus_commission
FROM employees;

This query retrieves the first_name and last_name columns from the employees table and returns the minimum value between the bonus and commission columns as min_bonus_commission.

MySQL LEAST WITH MULTIPLE COLUMNS

Step 6: Using LEAST with Constants

Use the LEAST function with constants:

SELECT first_name, last_name, LEAST(salary, 50000) AS salary_or_50000
FROM employees;

This query retrieves the first_name and last_name columns from the employees table and returns the smaller value between the salary column and the constant value 50000 as salary_or_50000.

MySQL LEAST WITH CONSTANTS

Conclusion

The MySQL LEAST function is a powerful tool for finding the minimum value among a set of values in SQL queries. Understanding how to use the LEAST function is essential for effective data querying and analysis in MySQL.