Tuesday 3 January 2012

What is difference betwen COUNT(1) and COUNT(*)?

There is NO difference between COUNT(1) and COUNT(*).
Both will return same number of rows.

One point to note is that count(columnname) is an aggregate function and does not take nulls into consideration.

So if table TABLE1 with one column COLUMN1 has 4 records and two of them have null values for COLUMN1 then SELECT COUNT(COLUMN1) will only return 2 and not 4.

  • Count(*) counts all occurrences including nulls. This is a row count.
  • Count (1) is like count(columns1) it will count all null values in a "pseudo column" created by the constant. Since the constant 1 will never be null, this would, like count(*), give you a row count.

COUNT(*) is row count and COUNT(1) is count of  constant pseudo column which cannot be null and hence in both cases the result will be same.



No comments:

Post a Comment