When working with data in SQL, there may be times when you need to calculate the average value of a column. In such cases, the AVG
function comes in handy.
Syntax
The basic syntax of the AVG
function is as follows:
SELECT AVG(column_name) FROM table_name;
Where column_name
is the name of the column for which you want to calculate the average, and table_name
is the name of the table from which you want to retrieve data.
Example
Let’s assume we have a table named students
with the following structure:
student_id | name | marks |
---|---|---|
1 | John | 90 |
2 | Alice | 85 |
3 | Michael | 95 |
4 | Emma | 70 |
5 | James | 80 |
To calculate the average marks of all the students, we can use the following SQL query:
SELECT AVG(marks) FROM students;
This query will return the average value of the marks
column, which in this case would be 84
.
Conclusion
The AVG
function in SQL allows you to easily calculate the average value of a column. By using this function, you can gain insights into your data and make informed decisions based on the average values. So next time you need to calculate an average in SQL, don’t forget to use the handy AVG
function!
#SQL #AVG