MySQL : MIN() and MAX() Functions

In MySQL, the MIN() and MAX() functions are aggregate functions used to find the smallest and largest values in a set of values, respectively. Here's how they work:

MIN() Function:

The MIN() function returns the smallest value of a selected column.

Syntax:

SELECT MIN(column_name) FROM table_name;
 

Example:

SELECT MIN(salary) FROM employees;

This query will return the minimum salary from the `employees` table.

MAX() Function:

The MAX() function returns the largest value of a selected column.

Syntax:

SELECT MAX(column_name) FROM table_name;
 

Example:

SELECT MAX(salary) FROM employees;

This query will return the maximum salary from the `employees` table.

Using with GROUP BY:

You can also use MIN() and MAX() functions with the GROUP BY clause to find the minimum or maximum values for each group.

Example:

SELECT department, MIN(salary) AS min_salary, MAX(salary) AS max_salary
FROM employees
GROUP BY department;


This query will return the minimum and maximum salary for each department in the `employees` table.

Using with WHERE clause:

You can combine MIN() and MAX() functions with the WHERE clause to find the minimum or maximum values based on specific conditions.

Example:

SELECT MIN(salary) FROM employees WHERE department = 'Sales';

This query will return the minimum salary from the `employees` table for the 'Sales' department.

These functions are useful for various analytical purposes, such as finding the highest or lowest values in a dataset, determining salary ranges, or identifying outliers.