๐ What Youโll Learn
- What the
AVG()
function does - How to calculate average values
- Real-world examples
- Use with
GROUP BY
andWHERE
๐ What Is AVG?
The AVG()
function calculates the average (mean) of numeric values in a column.
It ignores NULL
values and works well for generating insights from data like average price, salary, or rating.
๐งฑ Basic Syntax
SELECT AVG(column_name) FROM table_name;
๐งช Example: Average Order Amount
SELECT AVG(amount) AS average_order
FROM orders;
๐งช Example: Filtered Average
Average order amount in 2024
SELECT AVG(amount) AS avg_2024_order
FROM orders
WHERE YEAR(order_date) = 2024;
๐ Example with GROUP BY
Average salary by department
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;
โ ๏ธ Notes
AVG()
ignoresNULL
values- Works only with numeric columns
- Round the result with
ROUND()
if needed:
SELECT ROUND(AVG(score), 2) FROM results;
๐ Recap
AVG()
calculates the mean of numeric data- Use it with
WHERE
andGROUP BY
for flexibility - It excludes
NULL
values - Combine with
ROUND()
for readable output