MIN Function - ( SQL Server Aggregate Function )
The MIN
is an aggregate function in SQL Server that is used to return the minimum value or smallest value from a given set of expression.
Syntax
The syntax of the MIN()
function is:
SELECT MIN(expression) FROM table_name WHERE conditions;
Where:
expression
: Specifies the column or expression to evaluate.table_name
: Specifies the table name to retrieve data from.conditions
: Specifies the conditions to filter data.
Example
Let's consider the following table named employees
:
emp_id | emp_name | emp_salary |
---|---|---|
1 | John | 50000 |
2 | Smith | 45000 |
3 | David | 55000 |
4 | Karen | 60000 |
Now, the following SQL query returns the employee(s) having the minimum salary:
SELECT emp_name, MIN(emp_salary) AS min_salary FROM employees;
The output of this query would be:
emp_name | min_salary |
---|---|
Smith | 45000 |
Explanation
The MIN()
function evaluates the specified expression or column for each row of the table and returns the minimum value. If the specified column contains the NULL values, then it returns the NULL value.
It is generally used with the GROUP BY
clause to obtain the minimum value for each group.
Use
The MIN()
function is useful for finding the lowest or minimum value from a given set of values.
It can be used for various purposes such as finding the lowest/highest scoring student, the oldest person, and so on.
Important Points
- The
MIN()
function is an aggregate function in SQL Server that returns the minimum value from a given set of values. - It is often used with the
GROUP BY
clause to obtain the minimum value for each group. - If the specified column contains the NULL values, then it returns the NULL value.
Summary
In this article, we discussed the MIN()
function, which is an aggregate function in SQL Server used to return the minimum or smallest value from a given set of values. We covered its syntax, example, output, explanation, use, and important points. By using the MIN()
function, you can easily retrieve the minimum or smallest value from a column or expression in your SQL Server database table.