SQL Query: How to Find Salary Dept wise

Here’s an interview question; asked in many interviews. The question: Write SQL query to find Salaries paid department-wise. You can get salaries paid to employees for each of the department codes from the EMPLOYEE table.

Read: 5 SQL MCQ Questions, Useful for Interviews As Well

Get Table Details

Take a look at this table by using the describe table command (DESC EMPLOYEE) or by using a full select statement (SELECT * FROM EMPLOYEE).

You can get the department code for an employee from the workdept column for the table and the relevant salary from the salary column of the same row.

Read: How to Derive Year from DATEs Useful MySQL Query

How to Write SQL Query

For arriving department-wise salary, you can use the SUM function. How do you tell the SUM function that you want different sums for each of the department codes?

Easy: you can tell by using work dept in GROUP BY. That’s the purpose of the group by clause.

Read: Join Three Tables with WHERE Here is Useful SQL Query

select workdept, sum(salary) as newsalary
from employee
group by workdept


D11                  646620.00
A00                  354250.00
B01                   94250.00
E11                  317140.00
4 record(s) selected.

Get new content delivered directly to your inbox.

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.