Pages

3 August 2013

SQL- COUNT(ColumnName) and COUNT(*)

Q.What is the difference between count(Column Name) and count(*)?

         Count(Column Name)- It returns the total number of rows in a table                                                    excluding NULL.

         Count(*)- It returns the total number of rows in a table  including NULL.
         
     Let us consider a below 'Customer' table. Column 'ProductId' of this table has two NULL values.


Customer Table



Now, a query which contains Count(*) has been executed. Count includes those records which has  NULL value. It returns 9 rows 



Now, a query which contains Count(Column Name) has been executed.Count does not include those records which .It returns 7 rows.


One very interesting fact about Count function is that Count(any constant number) will behave same as that of Count(*). Let us see. It also returned same result as that of Count(*)