In SQL, what's the difference between count(column) and count(*)?

Viewed 47416

I have the following query:

select column_name, count(column_name)
from table
group by column_name
having count(column_name) > 1;

What would be the difference if I replaced all calls to count(column_name) to count(*)?

This question was inspired by How do I find duplicate values in a table in Oracle?.


To clarify the accepted answer (and maybe my question), replacing count(column_name) with count(*) would return an extra row in the result that contains a null and the count of null values in the column.

12 Answers

count(*) counts NULLs and count(column) does not

[edit] added this code so that people can run it

create table #bla(id int,id2 int)
insert #bla values(null,null)
insert #bla values(1,null)
insert #bla values(null,1)
insert #bla values(1,null)
insert #bla values(null,1)
insert #bla values(1,null)
insert #bla values(null,null)

select count(*),count(id),count(id2)
from #bla

results 7 3 2

Another minor difference, between using * and a specific column, is that in the column case you can add the keyword DISTINCT, and restrict the count to distinct values:

select column_a, count(distinct column_b)
from table
group by column_a
having count(distinct column_b) > 1;

A further and perhaps subtle difference is that in some database implementations the count(*) is computed by looking at the indexes on the table in question rather than the actual data rows. Since no specific column is specified, there is no need to bother with the actual rows and their values (as there would be if you counted a specific column). Allowing the database to use the index data can be significantly faster than making it count "real" rows.

The explanation in the docs, helps to explain this:

COUNT(*) returns the number of items in a group, including NULL values and duplicates.

COUNT(expression) evaluates expression for each row in a group and returns the number of nonnull values.

So count(*) includes nulls, the other method doesn't.

COUNT(*) – Returns the total number of records in a table (Including NULL valued records).

COUNT(Column Name) – Returns the total number of Non-NULL records. It means that, it ignores counting NULL valued records in that particular column.

Further elaborating upon the answer given by @SQLMeance and @Brannon making use of GROUP BY clause which has been mentioned by OP but not present in answer by @SQLMenace

CREATE TABLE table1 ( 
id INT 
);
INSERT INTO table1 VALUES 
(1), 
(2), 
(NULL), 
(2), 
(NULL), 
(3), 
(1), 
(4), 
(NULL), 
(2);
SELECT * FROM table1;
+------+
| id   |
+------+
|    1 |
|    2 |
| NULL |
|    2 |
| NULL |
|    3 |
|    1 |
|    4 |
| NULL |
|    2 |
+------+
10 rows in set (0.00 sec)
SELECT id, COUNT(*) FROM table1 GROUP BY id;
+------+----------+
| id   | COUNT(*) |
+------+----------+
|    1 |        2 |
|    2 |        3 |
| NULL |        3 |
|    3 |        1 |
|    4 |        1 |
+------+----------+
5 rows in set (0.00 sec)

Here, COUNT(*) counts the number of occurrences of each type of id including NULL

SELECT id, COUNT(id) FROM table1 GROUP BY id;
+------+-----------+
| id   | COUNT(id) |
+------+-----------+
|    1 |         2 |
|    2 |         3 |
| NULL |         0 |
|    3 |         1 |
|    4 |         1 |
+------+-----------+
5 rows in set (0.00 sec)

Here, COUNT(id) counts the number of occurrences of each type of id but does not count the number of occurrences of NULL

SELECT id, COUNT(DISTINCT id) FROM table1 GROUP BY id;
+------+--------------------+
| id   | COUNT(DISTINCT id) |
+------+--------------------+
| NULL |                  0 |
|    1 |                  1 |
|    2 |                  1 |
|    3 |                  1 |
|    4 |                  1 |
+------+--------------------+
5 rows in set (0.00 sec)

Here, COUNT(DISTINCT id) counts the number of occurrences of each type of id only once (does not count duplicates) and also does not count the number of occurrences of NULL

It is best to use

Count(1) in place of column name or * 

to count the number of rows in a table, it is faster than any format because it never go to check the column name into table exists or not

Related