MySQL GREATEST() Function



MySQL GREATEST() Function

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


Syntax

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

The GREATEST 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 GREATEST Function

Let's look at some examples of the MySQL GREATEST 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 GREATEST with WHERE Clause

Use the GREATEST function to find the maximum value in a list of expressions:

SELECT first_name, last_name, GREATEST(salary, bonus, commission) AS highest_income
FROM employees;

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

MySQL GREATEST WITH WHERE CLAUSE

Step 5: Using GREATEST with Multiple Columns

Use the GREATEST function with multiple columns:

SELECT first_name, last_name, GREATEST(bonus, commission) AS max_bonus_commission
FROM employees;

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

MySQL GREATEST WITH MULTIPLE COLUMNS

Step 6: Using GREATEST with Constants

Use the GREATEST function with constants:

SELECT first_name, last_name, GREATEST(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 greater value between the salary column and the constant value 50000 as salary_or_50000.

MySQL GREATEST WITH CONSTANTS

Conclusion

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