SQL's COUNT Function: How COUNT(*), COUNT(column), and COUNT(DISTINCT) Differ
The SQL COUNT function has three distinct forms that behave differently and commonly confuse developers in interviews and on the job. COUNT(*) tallies every row in a table regardless of NULL values, while COUNT(column) only counts rows where the specified column is not NULL. COUNT(DISTINCT column) goes further by counting only unique non-NULL values in that column. The gap between COUNT(*) and COUNT(column) is a practical way to measure missing data in a dataset. Comparing COUNT(*) with COUNT(DISTINCT column) also serves as a quick method to detect duplicate records in a table.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.



Discussion (0)
Log in to join the discussion and vote.
Log in