Understanding the AVG() Aggregate Function in SQL: A Complete Guide

Rumman Ansari   Software Engineer   2024-07-19 02:09:30   5609  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

The AVG() function returns the average value of a numeric column.

The SUM() function returns the total sum of a numeric column.

Syntax: AVG()

<span class="pln">
SELECT AVG</span><span class="pun">(</span><span class="pln">column_name</span><span class="pun">)</span><span class="pln">
FROM table_name
WHERE condition</span><span class="pun">;</span><span class="pln">
</span>

We will apply AVG function in this below table:

We will find the AVG employee salary

EmpId

EmpName

EmpAddress

EmpSalary

EmpDept

11

Rambo

Kolkata

30000

ADMIN

21

Inza

Bihar

31000

SALES

32

Samser

Kolkata

32000

IT

41

Kamran

Hydrabad

33000

ITIS

52

Azam

Kolkata

33000

ITIS

Example: COUNT

Code:

<span class="pln">
SELECT AVG</span><span class="pun">(</span><span class="typ">EmpSalary</span><span class="pun">)</span><span class="pln"> AS </span><span class="typ">AvgSalary</span><span class="pln"> FROM </span><span class="typ">EmployeeDetails</span><span class="pln">
</span>

You have to remember EmpSalary is a numeric column

Output:

The above code will produce the following result-

<span class="pln">
</span><span class="typ">AvgSalary</span><span class="pln">
</span><span class="lit">31800</span><span class="pln">
</span>

Code: Required Code to create the table

<span class="pln">
USE </span><span class="typ">SQLExamples</span><span class="pln">

DROP TABLE </span><span class="typ">EmployeeDetails</span><span class="pln">

CREATE TABLE </span><span class="typ">EmployeeDetails</span><span class="pun">(</span><span class="pln">
</span><span class="typ">EmpId</span><span class="pln"> </span><span class="kwd">int</span><span class="pun">,</span><span class="pln">
</span><span class="typ">EmpName</span><span class="pln"> VARCHAR</span><span class="pun">(</span><span class="lit">30</span><span class="pun">),</span><span class="pln">
</span><span class="typ">EmpAddress</span><span class="pln"> VARCHAR</span><span class="pun">(</span><span class="lit">50</span><span class="pun">),</span><span class="pln">
</span><span class="typ">EmpSalary</span><span class="pln"> </span><span class="kwd">int</span><span class="pun">,</span><span class="pln">
</span><span class="typ">EmpDept</span><span class="pln"> VARCHAR</span><span class="pun">(</span><span class="lit">10</span><span class="pun">)</span><span class="pln">
</span><span class="pun">)</span><span class="pln">

INSERT INTO </span><span class="typ">EmployeeDetails</span><span class="pln"> VALUES
</span><span class="pun">(</span><span class="lit">11</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Rambo'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Kolkata'</span><span class="pun">,</span><span class="pln"> </span><span class="lit">30000</span><span class="pun">,</span><span class="pln"> </span><span class="str">'ADMIN'</span><span class="pun">),</span><span class="pln">
</span><span class="pun">(</span><span class="lit">21</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Inza'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Bihar'</span><span class="pun">,</span><span class="pln"> </span><span class="lit">31000</span><span class="pun">,</span><span class="pln"> </span><span class="str">'SALES'</span><span class="pun">),</span><span class="pln">
</span><span class="pun">(</span><span class="lit">32</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Samser'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Kolkata'</span><span class="pun">,</span><span class="pln"> </span><span class="lit">32000</span><span class="pun">,</span><span class="pln"> </span><span class="str">'IT'</span><span class="pun">),</span><span class="pln">
</span><span class="pun">(</span><span class="lit">41</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Kamran'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Hydrabad'</span><span class="pun">,</span><span class="pln"> </span><span class="lit">33000</span><span class="pun">,</span><span class="pln"> </span><span class="str">'ITIS'</span><span class="pun">),</span><span class="pln">
</span><span class="pun">(</span><span class="lit">52</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Azam'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'Kolkata'</span><span class="pun">,</span><span class="pln"> </span><span class="lit">33000</span><span class="pun">,</span><span class="pln"> </span><span class="str">'ITIS'</span><span class="pun">)</span><span class="pln">

SELECT </span><span class="pun">*</span><span class="pln"> FROM </span><span class="typ">EmployeeDetails</span><span class="pln">
</span>

No Questions Data Available.
No Program Data.

Stay Ahead of the Curve! Check out these trending topics and sharpen your skills.