SQL-Server-Count()-Avg()-And-Sum()-Functions .

In this session i have discussed the SQL Server Count() Avg() and Sum() function .


The COUNT()- It is used to returns the number of rows from the table . If you have any condition then you can also specify .


The AVG()- It is used to returns the avarage value of specified numeric column from the table . If you have any condition then you can also specify .


The SUM()- It is used to returns the sum of specified numeric column from the table . If you have any condition then you can also specify .


COUNT() Syntax .

The below code specify how to use the COUNT() function .

    SELECT COUNT(column_name) FROM Table_Name 
    

AVG() Syntax .

The below code specify how to use the AVG() function .

    SELECT AVG(column_name) FROM table_Name 
    

SUM() Syntax .

The below code specify how to use the SUM() function .

    SELECT SUM(column_name) FROM table_Name 
    

Database Demo

The following data shown by Employee table of abc database .

EmpID EmpName EmpCity EmpEmail EmpSalary
1 Amit Noida amit@gmail.com 3500
2 Amita Noida amita@gmail.com 4000
3 Amita Delhi amita123@gmail.com 3000
4 Shashank Noida shashank@gmail.com 2000
5 Chanchal Gorahpur chanchal@gmail.com 4500

COUNT() function Example .

The below SQL Server statement display the 5 rows from Employee table.

    SELECT COUNT(*) AS CountRows FROM Employee
    

The output is look like below .

CountRows
5

AVG() function Example .

The below SQL Server statement display the avarage of salary from Employee table.

    SELECT AVG(EmpSalary) AS AvgEmpSalay FROM Employee
    

The output is look like below .

AvgEmpSalary
3400

SUM() function Example .

The below SQL Server statement display the sum of salary from Employee table.

    SELECT SUM(EmpSalary) AS sumEmpSalay FROM Employee
    

The output is look like below .

SumEmpSalary
17000

.

Welcome to AndroidATPoint.

back image                     next image