SQL TutorialLesson 20Free preview
SQL Avg()
This lesson is part of SQL · All levels
Summary
The AVG function calculates the average value of the numerical column by summing all the values and dividing it by the number of rows that have the value. NULL does not participate in addition or division. If you want to treat NULL as zero, use COALESCE first.
Key points
- AVG(column) is equal to the sum of the values divided by the number of non-NULL rows.
- It is not counted as zero; the line is passed by itself.
- Can be used with GROUP BY for average per group.
- To treat NULL as zero: AVG(COALESCE(column, 0)).
- The AVG result is a decimal number; use the ROUND to round it.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.