Search This Blog

Friday, December 20, 2013

MAX SQL Server Example

SQL Server > Built-in Functions > MAX

MAX returns the maximum 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 MAX(Salary) Max_Salary_In_Company from #LocalTempTable
select MAX(Salary) Max_Salary_By_Dept, DeptId from #LocalTempTable group by DeptId

drop table #LocalTempTable