SQL

SQL AVERAGE Function: Syntax, Usage, and Examples

The SQL AVERAGE function calculates the mean value of a numeric column in a database. It’s commonly used to analyze data trends, report statistics, or evaluate performance metrics based on numerical values.

How to Use the SQL AVERAGE Function

You use the SQL AVERAGE function with the AVG() keyword. It works only on numeric columns and ignores NULL values automatically.

Syntax

SELECT AVG(column_name)
FROM table_name;

You can also include a WHERE clause to filter rows or a GROUP BY clause to calculate averages for each category.

Example

SELECT AVG(price) AS average_price
FROM products;

This query returns the average price from the products table.

When to Use the AVERAGE Function SQL

Use the SQL AVERAGE function when you want to analyze numerical data in a meaningful way. It helps summarize values, compare performance, or generate insights.

1. Analyze Sales or Revenue

Calculate the average sale amount per transaction:

SELECT AVG(total_amount) AS avg_transaction
FROM sales;

This reveals how much customers typically spend.

2. Measure Customer Activity

Determine the average number of logins per user or interactions per session.

SELECT AVG(login_count)
FROM users;

This helps identify user engagement patterns.

3. Compare Performance by Category

Use it with GROUP BY to evaluate categories like departments, cities, or product lines.

SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;

This shows how different departments compare in average salaries.

Examples of SQL AVERAGE Function in Action

Example 1: Average Score in a Class

SELECT AVG(score) AS average_score
FROM student_scores;

Returns the class average for a test or assignment.

Example 2: Use with WHERE Clause

SELECT AVG(salary)
FROM employees
WHERE department = 'Engineering';

Calculates the average salary for engineers only.

Example 3: Use in SQL Server with Aliases

SELECT AVG(duration) AS avg_minutes
FROM meetings;

Aliasing the result makes the output easier to read.

Example 4: Average by Date or Category

SELECT order_date, AVG(order_total) AS avg_daily_sales
FROM orders
GROUP BY order_date;

Provides a daily breakdown of average order totals.

Example 5: Average in Subqueries

SELECT name, salary
FROM employees
WHERE salary > (
  SELECT AVG(salary) FROM employees
);

Finds employees who earn more than the average salary.

Learn More About the AVERAGE Function SQL

SQL AVERAGE Function vs. SUM and COUNT

The AVERAGE function in SQL often works with SUM() and COUNT() behind the scenes.

SELECT SUM(score) / COUNT(score) AS avg_score
FROM exams;

This manually calculates the same value that AVG(score) returns. But AVG() makes it cleaner and easier to read.

Handling NULL Values

The SQL AVERAGE function ignores NULL values automatically. You don’t need to filter them out manually.

SELECT AVG(rating)
FROM reviews;

Even if some rows have no rating, the average still reflects only the non-null values.

Combine with ROUND() for Formatting

If your result returns many decimal places, use the ROUND() function to format the output.

SELECT ROUND(AVG(salary), 2) AS avg_salary
FROM employees;

This rounds the result to two decimal places.

AVERAGE Function in SQL Queries with GROUP BY

When grouping data by a column, you can use the SQL AVERAGE function to compare group performance.

SELECT category, AVG(price) AS avg_price
FROM products
GROUP BY category;

This gives insight into how average prices vary across product categories.

Using AVG() with JOINs

You can use the SQL AVERAGE function in more complex queries, such as those involving joins.

SELECT departments.name, AVG(employees.salary) AS avg_salary
FROM employees
JOIN departments ON employees.department_id = departments.id
GROUP BY departments.name;

This lets you analyze averages across linked tables.

SQL Server AVERAGE Function Use Case

In SQL Server, the AVG() function behaves consistently with other databases. It works in views, stored procedures, and user-defined functions.

-- In a stored procedure
CREATE PROCEDURE GetAveragePrice
AS
BEGIN
  SELECT AVG(price) AS avg_price FROM products;
END;

You can reuse this logic as part of larger enterprise applications.

Use in Data Dashboards

Many BI tools use the SQL AVERAGE function behind the scenes for charts and dashboards. If you're building custom reports, you’ll likely use AVG() in base queries.

SELECT department, AVG(performance_score) AS avg_score
FROM employee_metrics
GROUP BY department;

You can feed this into a bar chart or KPI panel easily.

AVG() with Window Functions

Advanced users can combine the SQL AVERAGE function with window functions for rolling or partitioned averages.

SELECT
  employee_id,
  department,
  AVG(salary) OVER (PARTITION BY department) AS dept_avg
FROM employees;

This shows the department average next to each employee's data.

Learn SQL for Free
Start learning now
button icon
To advance beyond this tutorial and learn SQL by doing, try the interactive experience of Mimo. Whether you're starting from scratch or brushing up your coding skills, Mimo helps you take your coding journey above and beyond.

Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.

You can code, too.

© 2025 Mimo GmbH