AVG Function in SQL

The AVG is an aggregate function in SQL which returns the average of values of the integer column of the table.

Syntax of AVG Function

In the Structured Query Language, we use the AVG function with the columns of the table as shown in the following block:

In this syntax, we have to define the name and column of that table on which we want to perform the AVG function.

Example of AVG function

Here, we will create the new table through which we will perform the AVG function with the columns of the table:

The following shows the syntax to create the new table in SQL:

The following CREATE statement creates the Product_Details table for storing the price and quantity of products:

The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table:

The following SELECT statement displays the inserted records of the above Product_Details table:

104P110.250945NULL2022-04-30ClothsNULL
202P415.50045752022-01-28Electrical5
103P218.25025NULL2022-02-18Toys4
111P725.2505152021-12-25Cloths9
210P615.50050702021-10-15ElectricalNULL
212P819.7501102502022-01-28Cloths4
112P1010.2505508352022-04-11ToysNULL

Query 1: The following SELECT query uses the AVG function with the Product_Quantity column of the above Product_Details table:

Output:

Average_ofproductquantity
16.39

Query 2: The following SELECT query uses the AVG function with the Selling_Price column of the above Product_Details table:

This query returns the average of selling price products.

Output:

Average_ofsellingpriceproducts
249

Query 3: The following SELECT query uses the AVG function with the Product_Rating column of the above Product_Details table:

This query returns the average of values of Product_rating columns.

Output:

Average_ofofproductrating
5.5

AVG Function with WHERE clause

We can also use the WHERE clause with the AVG function which adds the values of filtered rows.

The syntax for using the AVG function with the WHERE clause is as follows:

Query 1: The following SELECT query uses the AVG function with WHERE clause in the above Product_Details table:

This query finds the average of the quantity of those products from above table whose product id is greater than 200.

Output:

Average_ofproduct>200
16.91

Query 2: The following SELECT query uses the AVG function with WHERE clause in the above Product_Details table:

This query finds the average the purchasing price of those products which are release on 2022-01-28.

Output:

162.5

AVG Function with DISTINCT clause

We can also use the DISTINCT clause with the AVG function which adds the distinct values of the column from the table.

The syntax for using the AVG function with the DISTINCT clause is as follows:

Query 1: The following SELECT query uses the AVG function with the DISTINCT clause in the above Product_Details table:

This query adds the quantity of distinct values of Product_quantity column and then finds the average.

Output:

Average_ofdistinctproduct
17.8

Query 2: The following SELECT query uses the AVG function with the DISTINCT clause of the above Product_Details table:

Output:

Average_ofdistinctproductrating
6