AVG Function

SQL · Aggregate Functions

Theory

AVG returns the arithmetic mean of a numeric column: the sum of the non-NULL values divided by the number of non-NULL values. With GROUP BY you get one average per group, such as the average salary per department.

The NULL rule matters a lot here. If one IT employee has salary NULL, AVG(salary) for IT divides by 1, not 2, so the NULL row is simply not part of the average. If you want missing values treated as zero, write AVG(COALESCE(salary, 0)). Another gotcha is integer division: in some databases (SQL Server, for example) the average of integer values may come back as an integer, so 7 and 8 give 7 instead of 7.5. Casting to a decimal first, as in AVG(CAST(salary AS DECIMAL(10,2))), avoids it. Use ROUND() to make the result readable.

Why we use it

The mean is the quickest way to describe 'typical' size: average order value, average delivery time, average salary per team. AVG computes it inside the database in a single pass and handles NULLs for you.

When to use it

Use AVG for numbers where the mean makes sense. When data is skewed by a few huge values (like salaries with one CEO), the median is often a better 'typical' value. Decide up front whether missing values should be ignored or counted as 0.

How it works

  1. Rows arrive after FROM and WHERE
  2. GROUP BY splits rows into groups
  3. Is the next value NULL? Yes: Ignore it completely. No: Add it to the sum and 1 to the count.
  4. Divide sum by count of non-NULL values
  5. Was the count 0? Yes: Return NULL. No: Return sum / count.
  6. One average per group

Syntax

AVG(column)

Example

SELECT AVG(salary) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic