SQL COUNT(), AVG() and SUM()
- SQL COUNT() Function :
The COUNT() function provides the number of rows that matches a specified condition.COUNT() Syntax –
SELECT COUNT(column_name) FROM table_name WHERE condition;
- SQL AVG() Function :
The AVG() function provides the average value of a numeric column.AVG() Syntax –
SELECT AVG(column_name) FROM table_name WHERE condition;
- SQL SUM() Function :
The SUM() function provides the total sum of a numeric column.SUM() Syntax –
SELECT SUM(column_name) FROM table_name WHERE condition;
Let us assume, we have a table “BeginnerTab”.
Name | City | Salary | ID | DOJ |
---|---|---|---|---|
Abc | Delhi | 4500 | 134 | 6-Aug |
Dfe | Noida | 6500 | 245 | 4-March |
Def | Jaipur | 5400 | 546 | 2-July |
Mno | Noida | 7800 | 432 | 7-June |
Jkl | Jaipur | 5400 | 768 | 9-July |
Lmn | Delhi | 7800 | 987 | 8-June |
Ijk | Jaipur | 6700 | 654 | 5-June |
- COUNT() Example :
The following SQL statement finds the number of Names in the “BeginnerTab” table :SELECT COUNT(Name) FROM BeginnerTab;
Output –
7
- AVG() Example :
The following SQL statement finds the average price of salary in the “BeginnerTab” table :SELECT AVG(Salary) FROM BeginnerTab;
Output –
6300
- SUM() Example :
The following SQL statement will find the sum of the Salary in the “BeginnerTab” table :SELECT SUM(Salary) FROM BeginnerTab;
Output –
44100