The AVG() function in SQL is used to calculate the average value of a numeric column. It is an aggregate function, meaning it computes a single result from a set of values. The AVG() function is useful for a variety of scenarios, such as finding the average salary of employees, calculating average order amounts, or determining average scores in a grading system. This chapter will explore the AVG() function in depth, from basic syntax to advanced use cases, including examples, code, and output explanations.
The basic syntax of the AVG()
function is straightforward. It is used with the SELECT
statement and takes a numeric column as an argument.
SELECT AVG(column_name)
FROM table_name
WHERE condition;
column_name
: The column whose values you want to average.table_name
: The table containing the data.condition
: An optional condition to filter the rows.Consider a table employees
with the following data:
id | name | department | salary |
---|---|---|---|
1 | John Doe | HR | 5000 |
2 | Jane Smith | IT | 6000 |
3 | David Green | Sales | 7000 |
4 | Sarah Brown | IT | 8000 |
5 | James White | Sales | 5500 |
To calculate the average salary of all employees:
SELECT AVG(salary) AS AverageSalary
FROM employees;
AverageSalary |
---|
6300 |
The query calculates the average salary of all employees, which is 6,300.
You can combine the AVG()
function with the WHERE
clause to filter the data and calculate the average for a specific subset of rows.
To calculate the average salary of employees in the IT
department:
SELECT AVG(salary) AS ITAverageSalary
FROM employees
WHERE department = 'IT';
IT Average Salary |
---|
7000 |
The query calculates the average salary of employees in the IT department, which is 7,000.
The GROUP BY
clause is often used with the AVG()
function to calculate the average for groups of data. This allows you to calculate averages for distinct categories or groups, such as departments or regions.
To calculate the average salary for each department:
SELECT department, AVG(salary) AS AverageSalary
FROM employees
GROUP BY department;
Department | Average Salary |
---|---|
HR | 5000 |
IT | 7000 |
Sales | 6250 |
The query groups employees by department and calculates the average salary for each department.
The DISTINCT
keyword can be used with the AVG()
function to calculate the average of unique values. This ensures that duplicate values are ignored in the calculation.
To calculate the average of unique salary values in the employees
table:
SELECT AVG(DISTINCT salary) AS AverageUniqueSalary
FROM employees;
Average Unique Salary |
---|
6375 |
The query calculates the average of distinct salary values, ignoring duplicates.
The AVG()
function automatically ignores NULL
values when calculating the average. This means that any NULL
values in the column will not affect the result.
Consider a modified employees
table where some salaries are NULL
:
id | name | department | salary |
---|---|---|---|
1 | John Doe | HR | 5000 |
2 | Jane Smith | IT | 6000 |
3 | David Green | Sales | NULL |
4 | Sarah Brown | IT | 8000 |
5 | James White | Sales | 5500 |
To calculate the average salary, ignoring NULL
values:
SELECT AVG(salary) AS AverageSalary
FROM employees;
Average Salary |
---|
6125 |
The NULL
value is ignored, and the average is calculated from the non-null salaries.
You can also use AVG()
in queries that involve multiple tables. By joining tables, you can calculate averages that depend on data from multiple sources.
Consider two tables: customers
and orders
.
Customer ID | Customer Name |
---|---|
1 | John Doe |
2 | Jane Smith |
Order ID | Customer ID | Order Amount |
---|---|---|
101 | 1 | 100 |
102 | 1 | 150 |
103 | 2 | 200 |
To calculate the average order amount for each customer:
SELECT customers.customer_name, AVG(orders.order_amount) AS AverageOrderAmount
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
GROUP BY customers.customer_name;
Customer Name | Average Order Amount |
---|---|
John Doe | 125 |
Jane Smith | 200 |
The query joins the customers
and orders
tables, groups by customer name, and calculates the average order amount for each customer.
Subqueries allow you to combine the AVG()
function with other complex queries. For example, you can calculate averages based on dynamically generated results.
To find employees whose salaries are higher than the average salary:
SELECT name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
Name | Salary |
---|---|
Sarah Brown | 8000 |
The subquery calculates the average salary, and the outer query selects employees who earn more than the average.
Although the AVG()
function is typically used for a single column, you can calculate the average of values from multiple columns by combining them in an expression.
Suppose the employees
table has an additional bonus
column:
ID | Name | Department | Salary | Bonus |
---|---|---|---|---|
1 | John Doe | HR | 5000 | 500 |
2 | Jane Smith | IT | 6000 | 600 |
3 | David Green | Sales | 7000 | 700 |
To calculate the average total compensation (salary + bonus):
SELECT AVG(salary + bonus) AS AverageCompensation
FROM employees;
Average Compensation |
---|
6350 |
The query calculates the average total compensation by summing the salary and bonus for each employee and then calculating the average.
When working with large datasets, performance can become a concern. Here are a few tips to optimize queries involving AVG()
:
GROUP BY
when necessary, as it can add overhead.AVG()
more efficient.The AVG() function in SQL is a powerful tool for calculating averages in a database. From simple averages of a column to more complex calculations involving conditions, grouping, and joins, the AVG() function allows you to analyze your data effectively. In this chapter, we covered everything you need to know about AVG(), including handling NULL values, using GROUP BY, combining it with DISTINCT, and even calculating averages across multiple columns. With this knowledge, you can confidently use the AVG() function to derive meaningful insights from your data. Happy coding !❤️