Example of AVG Function
For better understanding of AVG Functions we need a table on which we will perform some operations. So we will create Table called employees which consist of employee_id, name, salary,and department_id as Columns.
Query:
CREATE TABLE employees
(
employee_id INT PRIMARY KEY,
name VARCHAR(50),
salary DECIMAL(10,2),
department_id INT
);
Output:
After Inserting Some Data into the Employees Table, Our Table Looks:
Output:
Explanation: The image displays the employees table where we can check the 5 rows inserted.
Example 1: Average Salary of the Employees
The query provides the average of the employees salary in the whole table and name the column as average_salary.
Output:
Explanation: The image displays the query and its resultant table which is the average salary of all the employees in the table.
Example 2: Average Salary of the Employees on Department Basis
The query provides the average salary for each department with respect to the department_id and the average salary of the department column is named as average_salary_per_department .
Query:
Output:
Explanation: The image displays the query and its resultant table with the department_id and average_salary_per_department column.
Example 3: Average Salary of Over 40,000 of Departments
The query provides the average salary of employees in each department but only if its greater than 40,000.
SELECT department_id, AVG(salary) AS average_salary_per_department
FROM employees
GROUP BY department_id
HAVING average_salary_per_department > 40000;
Output:
Explanation: The image displays the query and its resultant table with the department_id and average_salary_per_department column which is greater than 40,000.
Example 4: Average Salary and Count of the Employee at the Same Time
The query provides the average salary name as average_salary along with the count of the employees name as employees_count in the employees table.
Query:
Output:
Explanation: The image displays the query and its resultant table with the employees_count and average_salary columns.
MariaDB AVG Function
MariaDB is an open-source relational database management system that is a of MySQL and is based on SQL(Structured query language). It is an improved version of MySQL and has various features, security, and performance when compared to MySQL.
The MariaDB AVG function is used to calculate the average of values present in the column. The average returned is a decimal value. It can also remove duplicate values before computing the average.
Syntax:
AVG ( [ALL | DISTINCT] expr )
Contact Us