Tuesday, February 25, 2014

how to get information of employee whose salary is less than the average salary of department

Are you looking for the below?



create Table tbllessons(username varchar(100),Dep varchar(100),Salary int)
Insert into tbllessons Select 'User1','Science',600
Insert into tbllessons Select 'User2','Science',1000
Insert into tbllessons Select 'User3','Science',10
Insert into tbllessons Select 'User4','History',100
Insert into tbllessons Select 'User5','History',200

;with cte
as
(
Select *, AVG(Salary)Over(PArtition by Dep) AvgSal From tbllessons
)Select * From cte where Salary<AvgSal

Drop table tbllessons


No comments:

Post a Comment