Difference between Count(*) & Count(1)

  • What is the difference between Count(*),Count(1).... in sql server

    ?

    for ex. A table contains 100

    select count(*) from emp

    select count(1) from emp

    select count(2) from emp

    All the three queries display the same result .then what is the difference between them?

    Tanx 😀

  • No practical difference. They both count the number of rows in the resultset.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • select count(*) from table

    and

    select count(1) from table

    will return the same results, but

    select count(column name) from table

    will return the number of non null columns

  • SeanF (5/14/2009)


    select count(column name) from table

    will return the number of non null columns

    More correctly it will return the number of rows in the table where the column specified is not null.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply