Database Basics
The score column of a result table contains six values: 50, NULL, 70, NULL, 90, 30. Which shows, in order, the result of SELECT COUNT(*), COUNT(score), AVG(score) FROM result?
- A6, 6, 40
- B6, 4, 40
- C6, 4, 60
Show answer and explanation
Answer: C. 6, 4, 60
COUNT(*) counts rows regardless of whether they contain NULL, so it is 6. With a column specified, as in COUNT(score), only rows where that column is not NULL are counted, so it is 4. AVG(score) divides the sum of the four non-NULL values, 50 + 70 + 90 + 30 = 240, by the number of non-NULL values, 4, giving 240 ÷ 4 = 60. Note that this differs from dividing the sum by the total number of rows, SUM(score) / COUNT(*) = 240 ÷ 6 = 40. To treat NULL as 0 points, replace it with COALESCE(score, 0) before aggregating.
- A. This is the result of treating NULL as 0. COUNT(score) does not count rows with NULL.
- B. This confuses AVG with the sum divided by the total number of rows, 6.
- C. Correct. There are 6 rows, 4 non-NULL values, and their average is 240 ÷ 4 = 60.