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
group by workdept
4 record(s) selected.