AVG() — Calculate the Average

The AVG() function returns the average value of a numeric column.
It works with numbers and is useful for calculating things like average age, average salary, etc.

Syntax

SELECT AVG(column) FROM table_name WHERE condition;

Example 1: Average Age of All Employees

SELECT AVG(age) FROM employees;

This returns the average age of all employees.

+----------+
| AVG(age) |
+----------+
|  28.5000 |
+----------+

Example 2: Average Age per Department

SELECT department, AVG(age)
FROM employees
GROUP BY department;

This returns the average age of employees for each department.

+------------+----------+
| department | AVG(age) |
+------------+----------+
| HR         |  26.0000 |
| IT         |  28.0000 |
| Sales      |  28.0000 |
| Marketing  |  29.5000 |
| Finance    |  30.7500 |
+------------+----------+

Example 2: Average Age of male employees

SELECT AVG(age) FROM employees where gender = 'male';

This returns the average age of all male employees only.

+----------+
| AVG(age) |
+----------+
|  28.9167 |
+----------+

Tip

  • AVG() ignores NULL values.
  • Only works with numeric columns.

The AVG() function helps you calculate average values, which is very useful for analyzing trends and making comparisons!