The AVG function computes the average of the values for the column or expression specified as an argument. We should not give AVG function on CHAR arguments.
This function operates only on numeric arguments. The following example calculates the average salary of each department:
SELECT WORKDEPT, AVG(SALARY)
GROUP BY WORKDEPT;
- The AVG function is the preferred method of calculating the average of a group of values.
- Although an average, in theory, is nothing more than a sum divided by a count, DB2 may not return equivalent values for AVG(COL_NAME) and SUM(COL_NAME)/COUNT(*).
The reason is that the COUNT function will count all rows regardless of value, whereas SUM ignores nulls.