Database Basics
The score column of an exam table contains four values: 80, NULL, 70, 90 (NULL means the person did not take the exam). What is the result of SELECT AVG(score) FROM exam?
- A60
- B80
- CNULL
Show answer and explanation
Answer: B. 80
SQL aggregate functions (SUM, AVG, MAX, MIN, and COUNT with a column specified) ignore NULL. AVG(score) is the average of the three non-NULL values: (80 + 70 + 90) ÷ 3 = 240 ÷ 3 = 80. Treating NULL as 0 and dividing by 4 would give 240 ÷ 4 = 60, but AVG does not calculate that way. To average with absentees counted as 0 points, replace NULL with 0 before aggregating, as in AVG(COALESCE(score, 0)). If all values are NULL, the result of AVG is NULL.
- A. This treats NULL as 0 and divides by 4. AVG ignores NULL.
- B. Correct. It is the average of the three non-NULL values: 240 ÷ 3 = 80.
- C. A single NULL makes the result NULL for operators such as +, but aggregate functions ignore NULL.