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

  • AWS Glue Crawler Issue with Dynamic S3 Folder Paths? Here’s the Complete Fix

    AWS Glue Crawler Issue with Dynamic S3 Folder Paths? Here’s the Complete Fix

    Learn how to fix AWS Glue crawler issues when S3 paths contain dynamically changing folders like hash values or UUIDs. Step-by-step beginner-friendly guide.

  • How to Create a Generic Stored Procedure for KPI Calculation (SQL + AWS Lambda)

    How to Create a Generic Stored Procedure for KPI Calculation (SQL + AWS Lambda)

    In modern data engineering, building scalable and reusable systems is essential. Writing separate SQL queries for every KPI quickly becomes messy and hard to maintain. A better approach?👉 Use a Generic Stored Procedure powered by Dynamic SQL, and trigger it using AWS Lambda. In this blog, you’ll learn: What is a Generic Stored Procedure? A…

  • Unlocking the Power of Databricks Genie: A Comprehensive Guide

    Unlocking the Power of Databricks Genie: A Comprehensive Guide

    Databricks Genie is a collaborative data engineering tool built on the Databricks Unified Analytics Platform, enhancing data analytics for businesses. Key features include collaborative workspaces, efficient data processing with Apache Spark, built-in machine learning capabilities, robust data visualization, seamless integration, and strong security measures, fostering informed decision-making.