2

Is there any difference between COUNT(*) and COUNT(attribute_name)?

I used count(attribute_name) as I thought that it would be specific hence the searching process would be easier. Is that true?

It would be great to see any example with sql code with my issue to help me understand better

GMB
  • 216,147
  • 25
  • 84
  • 135
  • 3
    Count(*) will return number of rows in the table. Count(colname) will return number of non null rows for that particular column – Somnath Ghosh Nov 06 '19 at 08:40
  • Possible duplicate of [Count property vs Count() method?](https://stackoverflow.com/questions/7969354/count-property-vs-count-method) – Ed Bangga Nov 06 '19 at 08:46

5 Answers5

3

Imagine this table:

enter image description here


select Count(TelephoneNumber) from Calls -- returns 3
select Count(*) from Calls -- returns 4

count(column_name) also counts duplicate values. Consider:

enter image description here

select Count(TelephoneNumber) from Calls -- returns 4
wserr
  • 436
  • 1
  • 3
  • 12
3

COUNT(*) counts all the records in the group.

COUNT(column_name) only counts non-null values.

There is also another typical expression, COUNT(DISTINCT column_name), that counts non-null distinct values.

Since you asked for it, here is a demo on DB Fiddlde:

with t as (
    select 1 x from dual
    union all select 1 from dual
    union all select null from dual
)
select count(*), count(x), count(distinct x) from t
COUNT(*) | COUNT(X) | COUNT(DISTINCTX)
-------: | -------: | ---------------:
       3 |        2 |                1
GMB
  • 216,147
  • 25
  • 84
  • 135
2

COUNT(*) will count all the rows. COUNT(column) will count non-NULLs only.

Your can use of COUNT(*) or COUNT(column) which should be based on the desired output only.

Consider below Example of employee table

ID        Name      Description
1         Raji      Smart
2         Rahi      Positive
3 
4         Falle     Smart

select count(*) from employee;

Count(*)
4

select count(name) from employee;

Count(Name)
3
Andrew
  • 3,632
  • 24
  • 64
  • 113
1

count() only counts non-null values. * references the complete row and as such never excludes any rows. count(attribute_name) only counts rows where that column is no null.

So this:

select count(attribute_name)
from the_table

is equivalent to:

select count(*)
from the_table
where attribute_name is not null
  • so that means if I use count(attribute_name), it will count all data in the table even if the row has no data. Right? – newbeginnner Nov 06 '19 at 08:58
  • No, the opposite: It will count all rows where the _column_ **does** contain data. Look at the second query –  Nov 06 '19 at 08:59
-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.

For more detail this link would help you understand.