WikiGalaxy

Personalize

Understanding SQL AVG Function

Introduction to AVG Function:

The SQL AVG function calculates the average value of a numeric column within a set of rows. It's an aggregate function that is commonly used to derive insights from datasets.

Basic Usage:

To use the AVG function, you simply need to specify the column name within the parentheses. For example, SELECT AVG(column_name) FROM table_name; returns the average value of the specified column.

Example Scenario:

Consider a table Employees with a column salary. To find the average salary, you would use the query: SELECT AVG(salary) FROM Employees;

Handling NULL Values:

The AVG function automatically ignores NULL values in the column, ensuring that they do not affect the calculation of the average.

Using AVG with GROUP BY:

The AVG function can be combined with the GROUP BY clause to calculate the average for each group of data. For example, SELECT department, AVG(salary) FROM Employees GROUP BY department; calculates the average salary per department.

Practical Example:

Let's consider a more complex scenario where you want to find the average salary of employees who have been with the company for more than 5 years:


SELECT AVG(salary) 
FROM Employees 
WHERE years_with_company > 5;
    

Conclusion:

The SQL AVG function is a powerful tool for performing statistical analysis on numerical data. By understanding its basic syntax and applications, you can effectively calculate averages in your SQL queries.

Console Output:

Average Salary: 65000

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025