The AVG() function returns the Average of numeric column. SQL AVG() ignores Null Values.
Syntax of SQL AVG()
AVG ( [ ALL | DISTINCT ] expression )
Example 1 of SQL AVG()
From the Sal column, below query will show the AVG of all the values which are less than 25000
SELECT AVG(sal) as "Average Sal"
FROM emp
WHERE sal < 25000;
Example 2 of SQL AVG()
Below is the test_table (Number is an int type column):
ID | Number |
---|---|
1 | 1 |
2 | 1 |
3 | 1 |
4 | 1 |
5 | 2 |
6 | 2 |
7 | 2 |
8 | 2 |
9 | 3 |
10 | 3 |
11 | 3 |
12 | 3 |
13 | 4 |
14 | 4 |
15 | 4 |
16 | 4 |
17 | 5 |
18 | 5 |
19 | 5 |
20 | 6 |
21 | 6 |
22 | 6 |
23 | 2 |
24 | 2 |
25 | 7 |
26 | 7 |
27 | 7 |
28 | 5 |
29 | 7 |
30 | 5 |
31 | 5 |
32 | 2 |
33 | 5 |
34 | 2 |
35 | 2 |
Below are the queries run on this above table with their results:
Select AVG(all Number) from test_table
Result:
3
Select AVG(distinct Number) from test_table
Result:
4
No comments:
Post a Comment