What does "select count(1) from table_name" on any database tables mean?

Nrj picture Nrj · Oct 8, 2008 · Viewed 164.2k times · Source

When we execute select count(*) from table_name it returns the number of rows.

What does count(1) do? What does 1 signify here? Is this the same as count(*) (as it gives the same result on execution)?

Answer

Jeffrey L Whitledge picture Jeffrey L Whitledge · Oct 8, 2008

The parameter to the COUNT function is an expression that is to be evaluated for each row. The COUNT function returns the number of rows for which the expression evaluates to a non-null value. ( * is a special expression that is not evaluated, it simply returns the number of rows.)

There are two additional modifiers for the expression: ALL and DISTINCT. These determine whether duplicates are discarded. Since ALL is the default, your example is the same as count(ALL 1), which means that duplicates are retained.

Since the expression "1" evaluates to non-null for every row, and since you are not removing duplicates, COUNT(1) should always return the same number as COUNT(*).