The SQL AVG() Function
The SQL AVG() function is used to calculate the average or mean value of a set of numeric values in a specified column. It is basically used to find the average of data points such as grades, sales amounts, or any other numeric data.
The average or mean value of a group of numerical values in a given column is determined using the SQL AVG() function. It is frequently used to calculate the average of various numeric data points, including grades, sales figures, and other numerical data.
Syntax for Oracle database with AVG() function:
Examples of sql AVG() function
Example 1: Calculate the Average of Numeric Values in a Column
SQL> SELECT AVG(Score) AS AverageScore
FROM Grades;
AVG(Score) calculates the average of all values in the “Score” column.
AS AverageScore provides an alias for the result column for clarity.
Example 2: Calculate the Average with a Conditional WHERE Clause
SQL> SELECT AVG(Score) AS AverageCourseScore
FROM Grades
WHERE CourseID = 101;
AVG(Score) calculates the average of “Score” values for rows where the “CourseID” is 101.
Example 3: Calculate the Average with Grouping
SQL> SELECT CourseID, AVG(Score) AS AverageCourseScore
FROM Grades
GROUP BY CourseID;
GROUP BY CourseID groups the grades by their course.
AVG(Score) calculates the average of “Score” values for each course.
Example 4: Calculate the Average with Joining Tables
SQL> SELECT Students.StudentName, AVG(Grades.Score) AS AverageScorePerStudent
FROM Students
INNER JOIN Grades ON Students.StudentID = Grades.StudentID
GROUP BY Students.StudentName;
INNER JOIN combines the “Students” and “Grades” tables based on the common “StudentID” column.
AVG(Grades.Score) calculates the average score for each student.
The above examples show you how to use the AVG() function in SQL to calculate average values for numeric data. The SQL AVG() function is a valuable performing data analysis in your database.