Question

I have a table of employees Salary named as empSalary.

empSalary

I want to calculate sum of salaries issued by each department. What comes to my mind is

Select sum(Salary) from empSalary where deptId = (Select deptId from empSalary)

This statement gives me 5100 which is the sum of Salary where deptId = 1. How is this possible using only sql query? Sorry for the question title as i was unable to find words.

Était-ce utile?

La solution

You want to GROUP BY the Department:

SELECT Dept_ID
      ,sum(Salary) Total_Salary
FROM empSalary
GROUP BY Dept_ID

Autres conseils

This is exact use of group by , you make groups and and each group you can calculate sum, average, min , max or count

select DeptID, Sum(Salary) as SumSalary
From empSalary
Group by DeptID

If I understood the question correctly, "group by" in SQL helps here.

      Select sum(Salary) from empSalary group by DeptID;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top