Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 798
Nrj Avatar asked Oct 08 '08 03:10

Nrj


People also ask

What does SELECT 1 mean in postgresql?

The statement 'select 1' from any table name means that it returns only 1. For example, If any table has 4 records then it will return 1 four times.

What is COUNT (*) and COUNT 1?

The difference is simple: COUNT(*) counts the number of rows produced by the query, whereas COUNT(1) counts the number of 1 values. Note that when you include a literal such as a number or a string in a query, this literal is "appended" or attached to every row that is produced by the FROM clause.

What is SELECT COUNT in SQL?

SQL SELECT COUNT(*) function SQL SELECT statement can be used along with COUNT(*) function to count and display the data values. The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).


1 Answers

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(*).

like image 169
Jeffrey L Whitledge Avatar answered Oct 12 '22 21:10

Jeffrey L Whitledge