SQL AVG() Function


The AVG() Function

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

SQL AVG() Syntax

SELECT AVG(column_name) FROM table_name

SQL AVG() Example

We have the following "Result" table:

Student_No
Subject_No
Year_Exam
Marks
ST101
SU03
1
69
ST102
SU01
1
61
ST101
SU04
1
70
ST101
SU05
1
87
ST103
SU01
2
51
ST103
SU03
1
59
ST103
SU01
3
56
ST108
SU01
1
78
ST105
SU05
2
98
Now we want to find the average value of the "Marks" fields.

We use the following SQL statement:

SELECT AVG(Marks) FROM result;

The result-set will look like this:

 AVG(Marks)
    69.8889
Now we want to find the every student  average Marks value.

We use the following SQL statement:

SELECT Student_No,AVG(Marks) FROM result GROUP BY Student_No;

The result-set will look like this:


Student_No
AVG(Marks)
ST101
75.3333
ST102
61.0000
ST103
55.3333
ST105
98.0000
ST108
78.0000

No comments:

Post a Comment