Aggregation & GROUP BY · SQL SQL

What will be the output of COUNT() versus COUNT(columnname) when the specified column contains several NULL values?

Practice question from Aggregation & GROUP BY (SQL SQL), with the correct answer and a step-by-step solution.

medium · MCQ · Q-5F1FE4

What will be the output of COUNT(*) versus COUNT(column_name) when the specified column contains several NULL values?

  1. A.COUNT(*) counts all rows in the group, whereas COUNT(column_name) excludes rows where column_name is NULL.Correct answer
  2. B.Both return the exact same count because SQL treats NULL as a value.
  3. C.COUNT(*) excludes rows with NULLs, whereas COUNT(column_name) includes them.
  4. D.Both return zero if any NULL values are present in the table.

Solution

COUNT(*) counts the total number of rows in a group regardless of contents or NULLs. COUNT(column_name) specifically counts only the non-null occurrences of that column.

More from Aggregation & GROUP BY

All of Aggregation & GROUP BY

Practise more SQL

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed