๐Ÿ“ What Youโ€™ll Learn

  • What the AVG() function does
  • How to calculate average values
  • Real-world examples
  • Use with GROUP BY and WHERE

๐Ÿ” 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() ignores NULL 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 and GROUP BY for flexibility
  • It excludes NULL values
  • Combine with ROUND() for readable output