SQL Min and Max
SQL provides aggregate functions MIN()
and MAX()
to find the smallest and largest values in a column, respectively. These functions are often used to identify extremes in a dataset.
MIN() Function
The MIN()
function returns the smallest value from a specified column.
SELECT MIN(column_name) AS MinimumValue
FROM table_name
WHERE condition;
MAX() Function
The MAX()
function returns the largest value from a specified column.
SELECT MAX(column_name) AS MaximumValue
FROM table_name
WHERE condition;
Syntax
The basic syntax for using MIN()
and MAX()
functions is:
SELECT MIN(column_name) AS MinimumValue, MAX(column_name) AS MaximumValue
FROM table_name
WHERE condition;
Examples
Here are some examples of using MIN()
and MAX()
functions:
-- Find the minimum and maximum salary of employees
SELECT MIN(Salary) AS LowestSalary, MAX(Salary) AS HighestSalary
FROM Employees;
-- Find the minimum and maximum product price
SELECT MIN(ProductPrice) AS LowestPrice, MAX(ProductPrice) AS HighestPrice
FROM Products;