MYSQL Tutorial



MySQL AVG()


MySQL AVG() Function

The AVG() function in MySQL calculates the average (mean) value of a numeric column. It sums all non-NULL values and divides by the count of those values.

Syntax:

AVG(column_name)
  

Works only on numeric columns (INT, FLOAT, DECIMAL, etc.). NULLs are ignored.

Example Queries:

-- Average salary of all employees
SELECT AVG(salary) AS avg_salary FROM employees;

-- Average order total
SELECT AVG(order_total) AS avg_order FROM orders;

-- Average salary in 'Sales' department
SELECT AVG(salary) AS avg_sales_salary
FROM employees
WHERE department = 'Sales';
  

Key Points:

  • AVG() only works on numeric data.
  • NULL values are skipped in the calculation.
  • Combine with WHERE to average a subset of rows.
  • Often paired with GROUP BY for per-group averages.

Example with GROUP BY:

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

This query shows the average salary for each department, giving insights into compensation distribution.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review