đ The AVG() function is an aggregate function in SQL that calculates the average (mean) value of a numeric column. It is commonly used for reporting, statistical analysis, performance evaluation, financial calculations, and business intelligence. The AVG() function works only with numeric data types and ignores NULL values during calculation.
đ What is AVG()?
The AVG() function computes the arithmetic mean by adding all numeric values in a column and dividing the total by the number of non- NULL values. It returns a single numeric value representing the average.
Information
đ Basic Syntax
AVG() Syntax
SELECT AVG(column_name)
FROM table_name
WHERE condition;đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Marks | AttendancePercentage |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 92 | 96 |
| 102 | Bob | Mathematics | 85 | 90 |
| 103 | Charlie | Physics | 78 | 88 |
| 104 | David | Computer Science | 95 | 98 |
| 105 | Eva | Mathematics | 88 | 92 |
1ī¸âŖ Calculating the Average
Calculate the average marks of all students.
Average Marks
SELECT AVG(Marks) AS AverageMarks
FROM Students;Result:
| AverageMarks |
|---|
| 87.6 |
2ī¸âŖ AVG() with WHERE
Use the WHERE clause to calculate the average for selected rows only.
Average Marks of Computer Science Students
SELECT AVG(Marks) AS AverageCSMarks
FROM Students
WHERE Department = 'Computer Science';3ī¸âŖ AVG() with DISTINCT
Use the DISTINCT keyword to calculate the average of unique values only.
Average of Distinct Marks
SELECT AVG(DISTINCT Marks) AS UniqueAverage
FROM Students;Tip
đ AVG() with GROUP BY
The GROUP BY clause groups records, and AVG() calculates the average for each group.
Average Marks by Department
SELECT Department,
AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department;Sample Result:
| Department | AverageMarks |
|---|---|
| Computer Science | 93.5 |
| Mathematics | 86.5 |
| Physics | 78.0 |
đ AVG() with HAVING
The HAVING clause filters grouped results after averages have been calculated.
Departments with Average Marks Above 85
SELECT Department,
AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department
HAVING AVG(Marks) > 85;đ AVG() with JOIN
The AVG() function is frequently combined with joins to calculate averages across related tables.
Average Course Score per Student
SELECT s.Name,
AVG(c.Score) AS AverageScore
FROM Students s
JOIN Courses c
ON s.StudentID = c.StudentID
GROUP BY s.Name;đ§Ž AVG() with Expressions
You can calculate the average of an expression instead of a single column.
Average Marks After Bonus
SELECT AVG(Marks + 5) AS AverageBonusMarks
FROM Students;â ī¸ AVG() and NULL Values
The AVG() function ignores NULL values when calculating the average. Only non- NULL values are included.
| Column Values | AVG() Result |
|---|---|
| 10, 20, NULL, 30 | 20 |
Important
đ Common Uses of AVG()
| Purpose | Example |
|---|---|
| Average Marks | AVG(Marks) |
| Average Salary | AVG(Salary) |
| Average Sales | AVG(SalesAmount) |
| Average Attendance | AVG(AttendancePercentage) |
| Department-wise Average | AVG(...) GROUP BY Department |
đŧ Real-World Example
A university administrator wants to prepare a report showing the average marks of students in each department.
Department-wise Average Marks
SELECT Department,
AVG(Marks) AS AverageMarks
FROM Students
GROUP BY Department
ORDER BY AverageMarks DESC;This query generates a report displaying the average marks for each department, sorted from the highest average to the lowest.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ AVG() calculates the arithmetic mean of numeric values.
- đ It ignores NULL values.
- đ AVG(DISTINCT Column) calculates the average of unique values only.
- đ It is commonly used with WHERE, GROUP BY, and HAVING.
- đ AVG() can be used with expressions and joins.
- đ It is widely used in analytics, reporting, and business intelligence.