Sunday, September 25, 2011

SELECT COUNT(*) vs COUNT(1) vs COUNT(ColumnName)

COUNT(*) – Number of records in the table regardless of NULL values and duplicates
COUNT(1) – Number of records in the table regardless of NULL values and duplicates **IMPORTANT NOTE: The 1 does NOT refer to an ordinal location of a column. This will not count the records in the first column of the table as COUNT(ColumnName) does.**
COUNT(ColumnName) or COUNT(ALL ColumnName) – Number of non-NULL values
COUNT(DISTINCT ColumnName) – Number of distinct non-NULL values