Understanding COUNT(*), COUNT(1), and COUNT(column) in SQL Server

Understanding COUNT(*), COUNT(1), and COUNT(column) in SQL Server

Today we  Provides an in-depth insight of the various variations of the COUNT function in SQL Server, namely, COUNT(*), COUNT(1) , and COUNT(column). It describes their functions, distinctions, and performances with real-life illustrations using example tables and data.

SQL Server COUNT introduction

The SQL COUNT function returns the number of items in a group. It is normally used in establishing the number of rows in a table or the number of rows that meet a certain criterion. The performance of COUNT however, depends on the argument passed as an input. Now, we can take a look at the variations available:

 

1.COUNT(*)



COUNT(*)returns the total number of rows in a table and this does not take into consideration NULL values in any of the columns. It merely enumerates the number of rows that are present.

Example:

First, let's create a sample table named Employees: 

  1. CREATE TABLE Employees (  
  2.     EmployeeID INT PRIMARY KEY,  
  3.     FirstName VARCHAR(50),  
  4.     LastName VARCHAR(50),  
  5.     Department VARCHAR(50),  
  6.     Salary DECIMAL(10, 2)  
  7. );  
 
  1. --data  
  2. INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary) VALUES  
  3. (1, 'John''Doe''Sales', 50000.00),  
  4. (2, 'Jane''Smith''Marketing', 60000.00),  
  5. (3, 'Robert''Jones''Sales', 55000.00),  
  6. (4, 'Michael''Brown''IT', 70000.00),  
  7. (5, 'Linda''Davis''Marketing', 62000.00);  
  --Table output  
  1. EmployeeID  FirstName                      LastName                       Department                     Salary  
  2. ----------- ------------------------------ ------------------------------ ------------------------------ ------------------------------  
  3. 1           John                           Doe                            Sales                          50000.00  
  4. 2           Jane                           Smith                          Marketing                      60000.00  
  5. 3           Robert                         Jones                          Sales                          55000.00  
  6. 4           Michael                        Brown                          IT                             70000.00  
  7. 5           Linda                          Davis                          Marketing                      62000.00  
 --query   

select COUNT(*) as TotalEmployees  from Employees    
  1.     
  2. --output    
  3.     
  4. TotalEmployees    
  5. --------------    
  6. 5    
  7.     
  8. (1 row affected)    
 COUNT(*) = 5 which is the total row number in Employees table.

2.COUNT(1)



COUNT(1) also returns the total number of the rows in a given table just like in COUNT(*) case. The value in the parentheses is just any constant value. SQL Server is never looking at the value, it is merely counting rows. In effect, it simply evaluates the expression 1 (which is never NULL) and counts it, per row.
 Example:

Using the same Employees table, let's use COUNT(1):

  1. --query  
  2. SELECT COUNT(1) AS TotalEmployees  
  3. FROM Employees;  
  4. --output  
  5.   
  6. TotalEmployees  
  7. --------------  
  8. 5  
  9.   
  10. (1 row affected)  

The results are identical to COUNT(*) In the majority of circumstances, COUNT(1) and COUNT(*) perform essentially the same.

3.COUNT(column)



COUNT(column) returns the number of rows, which do not have NULL in the column. It disregards rows in which the column has a NULL value.

Example:

Let's modify the Employees table to include a column that can contain NULL values:

  1. ALTER TABLE Employees  
  2. ADD PhoneNumber VARCHAR(20) NULL;  
  3. --  
  4. UPDATE Employees  
  5. SET PhoneNumber = '555-123-4567'  
  6. WHERE EmployeeID IN (1, 2, 4);  
  7. --  
  8. UPDATE Employees  
  9. SET PhoneNumber = NULL  
  10. WHERE EmployeeID IN (3, 5);  

Now, the Employees table looks like this:

  1. --data  
  2. EmployeeID  FirstName                      LastName                       Department                     Salary                         PhoneNumber  
  3. ----------- ------------------------------ ------------------------------ ------------------------------ ------------------------------ --------------------  
  4. 1           John                           Doe                            Sales                          50000.00                       555-123-4567  
  5. 2           Jane                           Smith                          Marketing                      60000.00                       555-123-4567  
  6. 3           Robert                         Jones                          Sales                          55000.00                       NULL  
  7. 4           Michael                        Brown                          IT                             70000.00                       555-123-4567  
  8. 5           Linda                          Davis                          Marketing                      62000.00                       NULL  
  9.   
  10. (5 rows affected)  
  1. --query  
  2. SELECT COUNT(PhoneNumber) AS EmployeesWithPhoneNumber  
  3. FROM Employees;  
  4.   
  5. --output  
  6.   
  7. EmployeesWithPhoneNumber  
  8. ------------------------  
  9. 3  
  10.   
  11. (1 row affected)  

COUNT(PhoneNumber) returned 3 because only 3 employees have a non-NULL value in the PhoneNumber column.

Key Differences 

| Function | Description | Ignores NULLs |

  • COUNT(*) : Returns the total number of rows in the table. | No 
  • COUNT(1) : Returns the total number of rows in the table. | No 
  • COUNT(column) : Returns the number of rows where the specified column is not NULL. | Yes 
Performance Considerations
The difference in the performance between COUNT(*) and COUNT(1) is, in most cases, insignificant. The query optimizer in SQL Server normally knows how to treat them equally. COUNT(column) can also be a bit slower, in case the column is not indexed, since it would have to see each value to see if it is NULL.

To count all of the rows in a table, it is normally preferred to use COUNT(*)or COUNT(1) . In case you need to count only these rows where a certain column is not NULL, then COUNT(column) is the right solution.

Use Cases Cases

COUNT(*) or COUNT(1): Finding out the total count of customers or orders or products or any other entity which is represented by a table.

Or  COUNT(column): Locating the number of customers that have submitted a valid email address (where the email address field may contain NULL), the number of products that carry a particular weight (where the weight field may contain NULL where the weight is not known), or the number of employees who have a performance review in file.

Conclusion


It is important to understand the difference between COUNT(*)COUNT(1) , and COUNT(column) when writing an effective, error-free SQL query. When to use which of these variations lies in whether or not you require that all rows be counted or just those that have non-NULL values in a given column. Although the COUNT(*) and COUNT(1)  functions are usually interchangeable because they count all rows, COUNT(column) allows one to count only non-NULL values, a functionality that is necessary in numerous practical applications.

0 Comments

Post Comment

Your email address will not be published. Required fields are marked *