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.

Was it helpful?

Solution

You want to GROUP BY the Department:

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

OTHER TIPS

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top