Suppose you have the following table
You need to find which Department has the maximum average salary in a single query
you can easy ly find average value of each department using aggregate function AVG()
But how to select only the department having the maximum average salary.In SQL SERVER
USE
select top 1 DeptName,MAX(MyAvg) AVARAGE_SALARY FROM(
select AVG(salary) as MyAvg,DeptName from [SalaryTable] group by Deptname
) temp
GROUP BY DeptName
order by AVARAGE_SALARY desc
You need to find which Department has the maximum average salary in a single query
you can easy ly find average value of each department using aggregate function AVG()
But how to select only the department having the maximum average salary.In SQL SERVER
USE
select top 1 DeptName,MAX(MyAvg) AVARAGE_SALARY FROM(
select AVG(salary) as MyAvg,DeptName from [SalaryTable] group by Deptname
) temp
GROUP BY DeptName
order by AVARAGE_SALARY desc
Comments
Post a Comment