×

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples.

The AVG() function is one of the aggregate functions in SQL. The AVG() function displays the average of the values mentioned in the expression. The AVG() function is a numeric function. The AVG() function allows only one parameter. The AVG() function ignore NULL values.

The syntax for the SELECT AVG() function is as follows:

SELECT Column_Name_1, Column_Name_2, Column_Name_3, AVG(Column_Name) FROM Table_Name WHERE Expression;

Let’s understand the AVG() function with the help of examples

Consider the already existing table, which has the following data:

Table Name:- D_Students

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakshi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485855
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580784
202225Akshay Agarwal858078889082845
202226Shwetali Bhagwat908085889080861
202227Mayuri Wagh808085808285824
202228Utkarsh Rokade858080908484845

Example 1: Execute a query to find the average of the student’s first-semester column from the D_Stundets table.

SELECT AVG(First_Sem) AS 'First Semester Average' FROM D_Students;

We displayed the student's first-semester column average in the above SELECT AVG() function query example

The output of the above query is as follows:

First Semester Average
83.1667
SQL SELECT AVG

Example 2: Execute a query to find the average of the student’s total column group by the department id.

SELECT Department_Id, AVG(Total) AS 'Total Average' FROM D_Students GROUP BY Department_Id;

In the above SELECT AVG() function query example, we displayed the student’s total column average group by department id.

The output of the above query is:

Department_IdTotal Average
184.2500
279.6667
385.6667
483.0000
582.5000
SQL SELECT AVG

Example 3: Execute a query to find the average student's first-semester to sixth-semester column group by the student name.

SELECT Student_Id, Student_Name, AVG(First_Sem + Second_Sem +Third_Sem + Fourth_Sem + Fifth_Sem + Sixth_Sem)/6 AS 'OverAll Average' FROM D_Students GROUP BY Student_Name;

In the above SELECT AVG() function query example, we display the student's first-semester to sixth-semester average group by the student name. We have used six columns as one parameter in the average function.

The output of the above query is as follows:

Student_IdStudent_Name                      OverAll Average
202224Aakash Jain77.66666667
202225Akshay Agarwal83.83333333
202221Anuja Wanare85.00000000
202223Anushka Sen74.66666667
202115Axar Patel84.83333333
202117Mahesh Kumbhar77.33333333
202227Mayuri Wagh82.00000000
202116Meena Mishra78.16666667
202220Prajwal Lokhande80.50000000
202118Sakshi Patil77.83333333
202226Shwetali Bhagwat85.50000000
202114Sonali Patole91.16666667
202119Sopan Bhore74.66666667
202228Utkarsh Rokade83.83333333
202112Vaibhav Lokhande85.66666667
202111Vaishnavi Patil90.83333333
202222Venkatesh Iyer89.83333333
202113Yash Dhull89.00000000
SQL SELECT AVG

Example 4: Execute a query to find the average of the student’s first-semester to sixth-semester column group by the student’s name and average greater than75.

SELECT Student_Id, Student_Name, AVG(First_Sem + Second_Sem +Third_Sem + Fourth_Sem + Fifth_Sem + Sixth_Sem)/6 AS 'OverAll Average' FROM D_Students GROUP BY Student_Name HAVING AVG(First_Sem + Second_Sem +Third_Sem + Fourth_Sem + Fifth_Sem + Sixth_Sem)/6 > 75;

In the above SELECT AVG() function query example, we display the student's first-semester to sixth-semester average group by the student name, and the average is greater than 75. The HAVING clause is used in the query.

The output of the above query is as follows:

Student_IdStudent_Name                      OverAll Average
202224Aakash Jain77.66666667
202225Akshay Agarwal83.83333333
202221Anuja Wanare85.00000000
202115Axar Patel84.83333333
202117Mahesh Kumbhar77.33333333
202227Mayuri Wagh82.00000000
202116Meena Mishra78.16666667
202220Prajwal Lokhande80.50000000
202118Sakshi Patil77.83333333
202226Shwetali Bhagwat85.50000000
202114Sonali Patole91.16666667
202228Utkarsh Rokade83.83333333
202112Vaibhav Lokhande85.66666667
202111Vaishnavi Patil90.83333333
202222Venkatesh Iyer89.83333333
202113Yash Dhull89.00000000
SQL SELECT AVG

Related Topics

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL FULL JOIN

In this section, we will help you understand the concept of the SQL FULL Join clause with a few examples. The SQL FULL Join query is executed to display the integrated...

3 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

Update Query in SQL

Update is an SQL command that is used to modify the data that in already present in database. Update is a command of DML. DML means Data Manipulation Language. Basically,...

3 minutes read.

SQL SELECT AND Operator

This SQL tutorial explains and helps us understand how to use the AND Operator in the SELECT query with examples. The AND Operator is used to fetch the table’s records if...

2 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

Truncate function in SQL

The TRUNCATE is a numeric function in SQL which truncates the number according to the particular decimal points. Syntax of TRUNCATE Function SELECT TRUNCATE(X, D) AS Alias_Name; In the TRUNCATE syntax, X...

4 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

WHERE Clause vs HAVING Clause

The WHERE Clause and HAVING clause filter the records in the Structured Query Language queries. The main difference between the WHERE clause and the HAVING clause is the WHERE clause...

5 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 minutes read.

SQL Top Clause

Top Clause: It returns the first ‘n’ number of rows as output. This Top clause is used to retrieve the required number of rows when there are thousands of records stored...

3 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.