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

What is the difference between COUNT(*), COUNT(1) and COUNT(ColumnName)? A mystery that will never be known… ok that was a lie, but the rest of this blog is not a lie, just to be clear.  🙂
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
I ran counts on a pretty good size table of 13+ million records and came up with both COUNT(*) and COUNT(1) executing with the same CPU time and elapsed time. Occasionally COUNT(*) would have a higher CPU time and sometimes COUNT(1) would have a higher CPU time. But neither was drastically different from the other. In addition to the statistics from the run if you look at the execution plans for both of these two they will be the exact same, providing further evidence that they behave the same. So from what I can conclude and have read from other sources online they are both essentially the same thing.
Conclusion: COUNT(*) and COUNT(1) are the same.
From what I understand this MAY have been an issue with Oracle where the query engine would treat them different, but I can’t confirm that just thought I would toss it out there for argument sake. Those same sources also say that has been resolved and they both function the same now. Also note that COUNT_BIG works exactly the same as COUNT it just returns the value in the form of a big integer instead of a regular integer.
Have you seen these same results or do you have evidence to the contrary?  Sound off in the comments!

About the author

Bradley Schacht

Bradley Schacht is a Principal Program Manager on the Microsoft Fabric product team based in Jacksonville, FL. Bradley is a former consultant, trainer, and has authored 5 SQL Server and Power BI books, most recently the Microsoft Power BI Quick Start Guide. As a member of the Microsoft Fabric product team, Bradley works directly with customers to solve some of their most complex data problems and helps shape the future of Microsoft Fabric. He frequently presents at community events around the country, is a contributor to sites such as SQLServerCentral.com, and is a member of the Jacksonville SQL Server User Group (JSSUG).

2 comments

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Bradley Schacht

Bradley Schacht is a Principal Program Manager on the Microsoft Fabric product team based in Jacksonville, FL. Bradley is a former consultant, trainer, and has authored 5 SQL Server and Power BI books, most recently the Microsoft Power BI Quick Start Guide. As a member of the Microsoft Fabric product team, Bradley works directly with customers to solve some of their most complex data problems and helps shape the future of Microsoft Fabric. He frequently presents at community events around the country, is a contributor to sites such as SQLServerCentral.com, and is a member of the Jacksonville SQL Server User Group (JSSUG).

Follow Me