August 15, 2011

Difference between COUNT(*),COUNT(Fieldname),COUNT(1) in SQL Server

We have 6 records in our table you can see it in below screen.

image

COUNT(*)

Count(*) gives the total number of records in a table.
I have used here count(*) and we are getting output 6.

image

COUNT(Fieldname)

If we are using COUNT(Fieldname) it will give total number of records but it will exclude null
values from result.
Here i am using COUNT(Location) it means output should be 4.
It is exactly what coming you can see in below screen.

image

COUNT(1)

There is nothing difference between COUNT(*) and COUNT(1).
See output below

image


That’s it.
Enjoy Reading.

No comments:

Post a Comment