SQL Aggregate Function MIN()
In SQL, the MIN()
function is an aggregate function that returns the minimum value in a set of values. The function takes a single argument, which is the name of the column or expression to be evaluated.
Here is an example of using the MIN()
function to find the lowest salary in a employees
table:
SELECT MIN(salary) AS min_salary FROM employees;
In this example, the MIN()
function is used to find the lowest value in the salary
column of the employees
table. The AS
keyword is used to alias the column name as min_salary
.
The MIN()
function can also be used with the GROUP BY
clause to find the minimum value for each group in a table. For example, consider the following sales
table:
+---------+-------+ | product | sales | +---------+-------+ | A | 100 | | A | 200 | | B | 150 | | B | 250 | +---------+-------+
To find the lowest sales value for each product, we can use the following query:
SELECT product, MIN(sales) AS min_sales FROM sales GROUP BY product;
In this example, the MIN()
function is used with the GROUP BY
clause to find the lowest sales value for each unique value in the product
column. The result of the query is a table with two columns: product
and min_sales
. The MIN()
function is applied to the sales
column for each group of rows that share the same product
value.