• da-zero (8/24/2010)


    What I meant was that the author should've included the COUNT function in the article, since it behaves differently when you specify additional arguments.

    E.g. I would've liked examples of COUNT(*), COUNT(expression), COUNT(DISTINCT expression) and COUNT(ALL expression).

    That would've made the article more comprehensive.

    I agree. Which is exactly why I included some information on this in my answer.

    To recap:

    COUNT(*) - how many rows?

    COUNT(column) - how many non-NULL values in the specified column?

    COUNT(DISTINCT column) - how many different non-NULL values in the specified column?

    COUNT(ALL column) - same as COUNT(column), since ALL is the default in the [ALL | DISTINCT] syntax.

    COUNT(expression) and COUNT(DISTINCT expression) are basicallly the same as COUNT([DISTINCT] column), except they operate on an expression rather than just a column reference.

    Example:

    If SELECT SomeColumn FROM SomeTable returns the values 1, 2, 2, 3, 5, NULL, 8, then SELECT COUNT(*), COUNT(SomeColumn), COUNT(DISTINCT SomeColumn) will return 7, 6, 5. Because there are 7 rows, 6 of which have a value for SomeColumn, but there are only 5 different values.


    Hugo Kornelis, SQL Server/Data Platform MVP (2006-2016)
    Visit my SQL Server blog: https://sqlserverfast.com/blog/
    SQL Server Execution Plan Reference: https://sqlserverfast.com/epr/