The SQL 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) FROM DSN81010.EMP 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.
Related posts
-
30 SQL Practice Queries with Solutions — Run Them Live
Practice 30 SQL queries with solutions, from SELECT and JOINs to subqueries and window functions. Run SQL queries live in an interactive SQL Playground.
-
Supercharge Your Database Skills: Try Our New Interactive SQL Plugin!
Struggling with database queries? Explore our new interactive SQL plugin to write, test, and master SQL directly in your browser. Try the live demo now!
-
How to Count Spark Stages by Sight and Optimize Shuffles
Want to predict execution stages before running a job? Learn how to count Spark stages by sight with code breakdowns, shuffle rules, and optimization traps.



