MAX() and MIN() function in SQL Server

 

MAX() function

The MAX() function returns the maximum value for a column in a table or set of rows returned in a query.


Summary:

SELECT MAX(Column_name)
FROM table_name

Example:

Suppose we have a table ‘tbl_employee’

Table name: tbl_Employee

Employee IdEmpNameSalaryDepartment
1Ankur1200Sales
2David3400Sales
3John1500HR
4James1200Production
5Mohan4500Production
6Ram2300HR

 

  1. Now we want to know maximum salary from above table then we can use following query with select statement:

SELECT MAX(salary) FROM tbl_Employee

And output will look like this:

4500

MIN() function

MIN() is the opposite of MAX() in every regard. The same rules and features of the MAX() function apply to the MIN() function. Important thing is that the lowest value returned for the text depends on the sort order and case sensitivity.

Summary:

SELECT MIN(Column_name)
FROM table_name

Example:

Suppose we have a table ‘tbl_employee’

Table name: tbl_Employee

Employee IdEmpNameSalaryDepartment
1Ankur1200Sales
2David3400Sales
3John1500HR
4James1200Production
5Mohan4500Production
6Ram2300HR

 

  1. Now we want to know minimum  salary from above table then we can use following query with select statement:

SELECT MIN(salary) FROM tbl_Employee

And output will look like this:

1200