Aggregate function AVG() in SQL

The AVG() function in SQL calculates the average value of a specified numeric column. It’s an aggregate function that’s incredibly useful for Data Analysis, helping to understand the central tendency of a data set, which can be critical in fields like user behaviour, finance, sales, and many others.

Syntax

To start calculating averages, you use the AVG() function within a SELECT statement, targeting the column of interest.

For example, to find the average user age we’d run the following query:

SELECT AVG(age)
FROM users

Using AVG() for detailed insights

In real life scenario, we’d further break down our average metric to groups, like average age per country. Here’s an example of a AVG() query with the GROUP BY statement:

SELECT
  country,
  AVG(age)
FROM users
GROUP BY country

Usually, we wouldn’t stop here and continue breaking down our groups with other dimensions (gender, occupation), etc.

Importance of AVG() function

The AVG() function is a fantastic way to deepen your understanding of SQL and Data Analysis.

It allows you to not only measure the average of a data set but also to slice the data in various ways for nuanced insights. Practice using AVG() in different contexts:

  • user demographics (average age per country, city, gender, signup source, etc)
  • revenue analysis (average purchase amount, average purchase items count, etc)
  • product analysis (average session duration, average page views, etc)

As you can see, the AVG() function is essential Data Analysis tool which you have to master.

Database compatibility

MySQL
PostgreSQL
SQLite
Redshift
Big Query
Snowflake
AVG
AVG
MySQL
PostgreSQL
SQLite
Redshift
Big Query
Snowflake

Find a problem on this page?

Want to get more involved? SQL MDN Docs is an open-source project, check out contribution guidelines.
This page was last modified on April 07, 2024.