Search This Blog

Friday, November 22, 2013

MIN function SQL Server Example

SQL Server > Built-in Functions > MIN

MIN returns the minimum value

Warning! Null values are ignored!

Example

CREATE TABLE #LocalTempTable(
       ID            int,
       Name   varchar(50),
       Salary int,
       DeptId int)

insert into #LocalTempTable(Id, Name, Salary, DeptId) values (1,'p1',100,1),  (2,'p2',200,1) ,  (2,'p3', null,2) ,   (2,'p4', 50,2)

select * from #LocalTempTable

select min(Salary) MIN_salary from #LocalTempTable
select min(Salary) MIN_salary, DeptId from #LocalTempTable group by DeptId

drop table #LocalTempTable
Results:

ID Name Salary DeptId
1 p1 100 1
2 p2 200 1
2 p3 NULL 2
2 p4 50 2

MIN_salary
50

MIN_salary DeptId
100 1
50 2