I often find these three variants:
SELECT COUNT(*) FROM Foo;
SELECT COUNT(1) FROM Foo;
SELECT COUNT(PrimaryKey) FROM Foo;
As far as I can see, they all do the same thing, and I find myself using the three in my codebase. However, I don’t like to do the same thing different ways. To which one should I stick? Is any one of them better than the two others?
Bottom Line
Use either
COUNT(field)orCOUNT(*), and stick with it consistently, and if your database allowsCOUNT(tableHere)orCOUNT(tableHere.*), use that.In short, don’t use
COUNT(1)for anything. It’s a one-trick pony, which rarely does what you want, and in those rare cases is equivalent tocount(*)Use
count(*)for countingUse
*for all your queries that need to count everything, even for joins, use*But don’t use
COUNT(*)for LEFT joins, as that will return 1 even if the subordinate table doesn’t match anything from parent tableDon’t be fooled by those advising that when using
*in COUNT, it fetches entire row from your table, saying that*is slow. The*onSELECT COUNT(*)andSELECT *has no bearing to each other, they are entirely different thing, they just share a common token, i.e.*.An alternate syntax
In fact, if it is not permitted to name a field as same as its table name, RDBMS language designer could give
COUNT(tableNameHere)the same semantics asCOUNT(*). Example:For counting rows we could have this:
And they could make it simpler:
And for LEFT JOINs, we could have this:
But they cannot do that (
COUNT(tableNameHere)) since SQL standard permits naming a field with the same name as its table name:Counting with null
And also, it is not a good practice to make a field nullable if its name matches the table name. Say you have values ‘Banana’, ‘Apple’, NULL, ‘Pears’ on
fruitfield. This will not count all rows, it will only yield 3, not 4Though some RDBMS do that sort of principle (for counting the table’s rows, it accepts table name as COUNT’s parameter), this will work in Postgresql (if there is no
subordinatefield in any of the two tables below, i.e. as long as there is no name conflict between field name and table name):But that could cause confusion later if we will add a
subordinatefield in the table, as it will count the field(which could be nullable), not the table rows.So to be on the safe side, use:
count(1): The one-trick ponyIn particular to
COUNT(1), it is a one-trick pony, it works well only on one table query:But when you use joins, that trick won’t work on multi-table queries without its semantics being confused, and in particular you cannot write:
So what’s the meaning of COUNT(1) here?
Is it this…?
Or this…?
By careful thought, you can infer that
COUNT(1)is the same asCOUNT(*), regardless of type of join. But for LEFT JOINs result, we cannot moldCOUNT(1)to work as:COUNT(subordinate.boss_id),COUNT(subordinate.*)So just use either of the following:
Works on Postgresql, it’s clear that you want to count the cardinality of the set
Another way to count the cardinality of the set, very English-like (just don’t make a column with a name same as its table name) : http://www.sqlfiddle.com/#!1/98515/7
You cannot do this: http://www.sqlfiddle.com/#!1/98515/8
You can do this, but this produces wrong result: http://www.sqlfiddle.com/#!1/98515/9