DB2 SQL Query to Find Salary Dept wise

ID-100221803Good SQL query example. It is asked in many interviews.

Suppose, that you want to know how much salary is paid to employees for each of the department codes listed in the SAMPLE database’s employee table. Take a look at this table in the Control Center by using the describe table command or by using a full select statement. 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. Although you can use the SUM function to do the arithmetic, how do you tell the SUM function that you want different sums for each of the department codes? Easy: you tell it to group its sums by each unique workdept value. That’s the purpose of the group by clause.

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

WORKDEPT NEWSALARY


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

Advertisements

Author: Srini

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