9. MySQL: MIN() AND MAX()

9. MySQL: MIN() AND MAX()

1. The MIN() function will return the smallest value in the mentioned column.

2. Likewise, the MAX() will return the largest value.

3. You can assign an alias to the value you extract and display.

MIN() AND MAX():

Syntax:

/*For MIN()*/
SELECT MIN(column_name)
FROM table_name
WHERE condition;

/*For MAX()*/
SELECT MAX(column_name)
FROM table_name
WHERE condition;

Let's see the table before getting the min and max values.

SELECT * FROM payment
LIMIT 5;

Output:

Example for MIN():

SELECT MIN(amount) as minAmount
FROM payment
WHERE staff_id = 1;

Output:

Example for MAX():

SELECT MAX(amount) as minAmount
FROM payment
WHERE staff_id = 1;

Output:

Note: These functions can also be used with other aggregate functions, like COUNT, SUM, AVG, etc., to return multiple values in a single query.