Tuesday, January 16, 2018

How To Use Avg() Functionality In Mssql

Hello Friend Today In This Blog I Will Show You How To Use Avg() By Functionality In Mssql.The AVG() function returns the average value of a numeric column.SELECT AVG returns the average of the data values.The SQL AVG function is used to return the average of an expression in a SELECT statement.This SQL tutorial explains how to use the SQL AVG function with syntax and examples.SQL AVG function is used to find out the average of a field in various records.SQL AVG() function calculates the average value of a column of numeric type. It returns the average of all non NULL values.In this tutorial, you will learn how to use the SQL AVG aggregate function to calculate the average of a set of numbers.The AVG function returns a single value whose data type is determined by the type of the result of the expression. The returned data type could be any numeric type such as integer, float, etc.Behind the scenes, the AVG function calculates the average of values by dividing the total of these values by the number of values except for the NULL values. Therefore, if the total of those values exceeds the maximum value of data type of the result, the database server will issue an error.The AVG function is one of an ANSI SQL aggregate functions, therefore, it is available in all relational database management systems e.g., Oracle, Microsoft SQL Server, MySQL, PostgreSQL, etc.AVG is a SQL aggregate function that calculates the average of a selected group of values. It’s very useful, but has some limitations. First, it can only be used on numerical columns. Second, it ignores nulls completely.

The AVG function works in a similar way as SUM. The difference is that the AVG function adds up or sums up all values and then calculates the average. The average is based on the number of records returned by the SQL statement, so you receive different results based on your WHERE clause. 

AVG() Syntax

SELECT AVG(column_name)
FROM table_name

WHERE condition;


Here Is An Example Related To Topic

Step 1 :Create database


Create database MNC




Step 2:Create table

Here In This step We Will Create table.

Here In This Step We Required Four Column With Name Uids ,Uname ,Ucountry and Usalary using Parameter bigint,varchar and float.

create table UserDetailinfo
(
Uids bigint,
Uname varchar(50),
Ucountry varchar(200),
Usalary float
);





Step 3 :Create Insert Trigger Functionality

Here In This Step We Will Insert Data Using Insert Command.
Data Will Be Added Manually Using Insert Command.


Insert Into UserDetailinfo values(1,'VIRAJ','INDIA',15000.00)
Insert Into UserDetailinfo values(2,'RAJ','USA',10000.00)
Insert Into UserDetailinfo values(3,'ANIL','UK',12000.00)
Insert Into UserDetailinfo values(4,'SUNIL','CANEDA',9000.00)
Insert Into UserDetailinfo values(5,'SHERYAS','CHINA',11000.00)
Insert Into UserDetailinfo values(6,'AJAY','CHINA',16000.00)


select *from UserDetailinfo






Step 4 :Create Avg() Functionality 

Here In This Step We Will Avg() Data Using Where Clause By Using Avg() Functionality .
Data Will Be Added Manually Using Avg() Functionality.
Here Average Of Data Is Found By Using Avg() Functionality By Passing Column Name Ucountry='CHINA'.


SELECT Avg(Usalary) As SALARYSUM
FROM UserDetailinfo
WHERE Ucountry='CHINA';




HERE IS THE VIDEO RELATED TO TOPIC


No comments:

Post a Comment