Sunday 1 July 2018

Difference Among Count(column_name), Count(*) and Count(1)

Below are the differences Among Count(column_name), Count(*) and Count(1).


Count(*) and Count(1) will give you total number of records in the table but Count(column_name) will give you NOT NULL records for the particular column selected .


For Example:-

I have a table called Employee, It has 3 columns and the data below.

Emp_name          Emp_id                Dept_name

Ram                         1                          IT
Shyaam                    2                          Sales
Seeta                        3                          IT
Geeta                       4                     


In the above table structure.

Scenario 1:-

Select count(*)
from employee;

select count(1)
from employee;

Select count(emp_id)
from employee;


results :-  4, 4 , 4

All the above queries will give same result.


Scenario 2:-

Select count(*)
from employee;

select count(1)
from employee;

Select count(dept_name)
from employee;


results :-  4, 4, 3

Count(1) and Count(*) will give you same result i.e. 4 but count(dept_name) will give you 3 as it counts only Not NULL values.

Hope this is clear now.

No comments:

Post a Comment