SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO SELECT

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL

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



ADVERTISEMENT
ADVERTISEMENT