📝 What You’ll Learn
- What the
AVG()function does - How to calculate average values
- Real-world examples
- Use with
GROUP BYandWHERE
🔍 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()ignoresNULLvalues- 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
WHEREandGROUP BYfor flexibility - It excludes
NULLvalues - Combine with
ROUND()for readable output