SQL SELECT AVG

The AVG is an aggregate function in SQL which calculates the average of values of the numerical column from the table.

Syntax of SQL Avg() Function

Two Examples of SQL Avg Function

Example 1: This example describes how to use the AVG function in the SQL table.

First, we have to create a new table in the database on which MIN function is to be run.

The following CREATE TABLE statement creates the Bikes_Details table with five fields:

The following INSERT query inserts the record of cars into the Bikes_Details table:

The following SELECT query shows the data ofthe Bikes_Details table:


Bike_NumberBike_ModelBike_NameNumber_of_BikesBike_Price
25782018KTM3900000
92582019Apache21100000
82332018Pulsar6900000
89902018RX1007700000
95782020Splender+68000000
12582021Buller21500000
25642019Passion46000000
21552020Livo81800000

The following query finds the average of values of the Bike_Price column from the above Bikes_Details table:

Output:

SQL SELECT AVG

SQL AVG Function With WHERE clause

The SQL Avg() function can also be used with the WHERE clause in the SELECT query for calculating the average of filtered values.

Syntax of AVG Function With WHERE clause

Example of SQL Avg Function with WHERE clause

The following query creates the College_Student_Details table using the CREATE TABLE statement:

The following SQL queries insert the record of students into the above table using INSERT INTO statement:

Let's see the record of the above table using the following SELECT statement:


Student_IDStudent_NameStudent_CourseStudent_AgeStudent_Marks
101AnujB.tech2088
102RamanMCA2498
104ShyamBBA1992
107VikashB.tech2088
111MonuMBA2165
114JonesB.tech1893
121ParulBCA2097
123DivyaB.tech2198
128HemantMBA2390
130NidhiBBA2065
132PriyaMBA2299
138MohitMCA2188

The following AVG query calculates the average of those Student_Marks which are greater than 90 in the above table:

Output:

SQL SELECT AVG

SQL AVG Function with DISTINCT clause

The AVG Function with Distinct clause in the SELECT statement calculates the average of distinct values of the column.

The syntax to use Distinct Clause with AVG Function is given below:

Example of AVG Function with DISTINCT Clause

We take the above College_Student_Details table to understand the concept of AVG with the DISTINCT Clause.

The following query calculates the average of distinct values of the Student_Marks column from the above College_Student_Details table:

Output:

SQL SELECT AVG

AVG Function with SQL GROUP BY clause

In some situations, we have to use the GROUP BY clause with the AVG function in the SELECT statement. The AVG Function with GROUP BY clause calculates the average of distinct values of the same group.

The syntax to use Distinct Clause with AVG Function is given below:

Example of AVG Function with GROUP BY Clause

Let's take the above College_Student_Details table to understand the concept of AVG with GROUP BY Clause.

The following query calculates the average student marks by course from the above College_Student_Details table:

Output:

Student_CourseAVG (Student_Marks)
B.tech91
MCA93
BBA78
MBA84
BCA97

Next TopicSQL SELECT MAX




Latest Courses