SQL Server Tutorial SQL Server Advanced SQL Server Database SQL Server References

SQL Server MIN() Function



The SQL Server (Transact-SQL) MIN() function returns the minimum value of an expression or specified column of a given table.

Syntax

The syntax for using MIN() function is given below:

SELECT MIN(column_name) 
FROM table_name
WHERE condition(s);

The SQL Server (Transact-SQL) GROUP BY clause is used to arrange result table into identical groups when one or more columns are used. Please note that it is must to include those column names in a GROUP BY clause which are not encapsulated within the MIN() function. See the syntax below:

SELECT column1, column2, ...
       MIN(column_name) 
FROM table_name
WHERE condition(s)
GROUP BY column1, column2, ...;

Parameters

column1, column2, ... Specify the column names that are not encapsulated within the MIN() function. It must be included in the GROUP BY clause.
column_name Specify the column or expression from which the minimum value need to be returned.
table_name Specify the table name from where the records need to retrieved.
WHERE condition(s) Optional. Specify the condition(s). Records are selected based upon specified condition(s).

Return Value

Returns the minimum value of a given expression.

Example - With Single Column

Consider a database table called Employee with the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

To fetch minimum Salary of the employees present in the Employee table, the query will be:

SELECT MIN(Salary) AS MinSalary 
FROM Employee;

This result of the above query will be:

MinSalary
2750


Example - Using GROUP BY

Consider the above discussed database named Employee which contains the following records:

EmpIDNameCityAgeSalary
1JohnLondon253000
2MarryNew York242750
3JoParis272800
4KimAmsterdam303100
5RameshNew Delhi283000
6HuangBeijing282800

To fetch the minimum Salary of the employees group by their age, where age is greater than 27, the following query can be used:

SELECT Age, MIN(Salary) AS MinSalary 
FROM Employee
WHERE Age > 27
GROUP BY Age;

This result of the above code will be:

AgeMinSalary
282800
303100

❮ SQL Server Functions