In SQL, does the aggregate COUNT() return the total number of rows in a table or result set, including rows that contain NULLs in any column?

Difficulty: Easy

Correct Answer: Applies — COUNT() counts all rows regardless of NULLs

Explanation:


Introduction / Context:
SQL provides aggregate functions for summarization. COUNT() is widely used, yet developers sometimes confuse it with COUNT(column). This question distinguishes these two behaviors.



Given Data / Assumptions:

  • COUNT() counts rows in the current scope of the query.
  • COUNT(column) counts non-NULL values in that column only.
  • Filters (WHERE) and grouping (GROUP BY) affect the counted set.



Concept / Approach:
COUNT() is defined to include every row that survives WHERE and JOIN conditions, independent of NULLs. It is the correct choice when you need the cardinality of the result set. Use COUNT(column) when you specifically want to ignore NULLs in that column.



Step-by-Step Solution:
Write the query with necessary joins and WHERE predicates.Use COUNT() to count the resulting rows.If you need to exclude rows where a column is NULL, use COUNT(column).Combine with GROUP BY for per-group counts.Verify by testing with rows that contain NULLs.



Verification / Alternative check:
Create a table with NULLs in several columns; COUNT() equals total rows, while COUNT(col) is lower when NULLs exist.



Why Other Options Are Wrong:
COUNT() never ignores rows due to NULLs. Primary keys or the presence of WHERE do not alter the definition.



Common Pitfalls:
Accidentally using COUNT(col) and misreporting totals; forgetting that joins can duplicate rows before counting.



Final Answer:
Applies — COUNT(*) counts all rows regardless of NULLs

More Questions from Database Design Using Normalization

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion