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

  • The End-to-End AI Stack – A Real Guide for Developers to Code, Create, and Execute

    The End-to-End AI Stack – A Real Guide for Developers to Code, Create, and Execute

    Learn how modern AI tools like ChatGPT, NotebookLM, and Antigravity fit into the AI stack. Discover the 5-layer AI capability model and how to choose the right AI tools for thinking, creating, building, and automat

  • FAANG-Style SQL Interview Traps (And How to Avoid Them)

    FAANG-Style SQL Interview Traps (And How to Avoid Them)

    SQL interviews at FAANG (Facebook/Meta, Amazon, Apple, Netflix, Google) are not about syntax. They are designed to test logical thinking, edge cases, execution order, and data correctness at scale. Many strong candidates fail—not because they don’t know SQL, but because they fall into subtle traps. In this blog, we’ll walk through real FAANG-style SQL traps,…

  • Common Databricks Pipeline Errors, How to Fix Them, and Where to Optimize

    Common Databricks Pipeline Errors, How to Fix Them, and Where to Optimize

    Introduction Databricks has become a premier platform for data engineering, especially with its robust integration of Apache Spark and Delta Lake. However, even experienced data engineers encounter challenges when building and maintaining pipelines. In this blog post, we’ll explore common Databricks pipeline errors, provide practical fixes, and discuss performance optimization strategies to ensure your data…