SQL Server: COUNT Function

COUNT function in SQL Server

This SQL Server tutorial explains how to use the COUNT function in SQL Server (Transact-SQL) with syntax and examples.

Description

In SQL Server (Transact-SQL), the COUNT function returns the count of an expression.

Syntax

The syntax for the COUNT function in SQL Server (Transact-SQL) is:

SELECT COUNT(aggregate_expression)
FROM tables
[WHERE conditions];

OR the syntax for the COUNT function when grouping the results by one or more columns is:

SELECT expression1, expression2, ... expression_n,
       COUNT(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;

Parameters or Arguments

expression1, expression2, … expression_n

Expressions that are not encapsulated within the COUNT function and must be included in the GROUP BY clause at the end of the SQL statement.

aggregate_expression

This is the column or expression whose non-null values will be counted.tablesThe tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.

WHERE conditions

Optional. These are conditions that must be met for the records to be selected.

Only includes NOT NULL Values

Not everyone realizes this, but the COUNT function will only include the records in the count where the value of expression in COUNT(expression) is NOT NULL. When expression contains a NULL value, it is not included in the COUNT calculations.

Let’s look at a COUNT function example that demonstrates how NULL values are evaluated by the COUNT function.

For example, if you have the following table called websites:

website_idurlwebsite_name
1 www.adglob.in Aglob
2 
3 

And if you ran the following SELECT statement that uses the COUNT function:

SELECT COUNT(website_id)
FROM websites;

Result: 3

This COUNT example will return 3 since all website_id values in the query’s result set are NOT NULL.

However, if you ran the next SELECT statement that uses the COUNT function:

SELECT COUNT(website_name) 
FROM websites;

Result: 1

This COUNT example will only return 1, since only one website_name value in the query’s result set is NOT NULL. That would be the first row where the website_name = ‘adglob’. It is the only row that is included in the COUNT function calculation.

Applies To

The COUNT function can be used in the following versions of SQL Server (Transact-SQL):

  • SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005

Example – With Single Field

Let’s look at some SQL Server COUNT function examples and explore how to use the COUNT function in SQL Server (Transact-SQL).

For example, you might wish to know how many contacts have a last_name of ‘Anderson’.

SELECT COUNT(*) AS "Number of contacts"
FROM contacts
WHERE last_name = 'Anderson';

In this COUNT function example, we’ve aliased the COUNT(*) expression as “Number of contacts”. As a result, “Number of contacts” will display as the field name when the result set is returned.

Example – Using DISTINCT

You can use the DISTINCT clause within the COUNT function. For example, the SQL statement below returns the number of unique departments where at least one employee has a first_name of ‘John’.

SELECT COUNT(DISTINCT department) AS "Unique departments"
FROM employees
WHERE first_name = 'John';

Again, the COUNT(DISTINCT department) field is aliased as “Unique departments”. This is the field name that will display in the result set.

Example – Using GROUP BY

In some cases, you will be required to use the GROUP BY clause with the COUNT function.

For example, you could also use the COUNT function to return the name of the department and the number of employees (in the associated department) that are in the state of ‘CA’.

SELECT department, COUNT(*) AS "Number of employees"
FROM employees
WHERE state = 'CA'
GROUP BY department;

Because you have listed one column in your SELECT statement that is not encapsulated in the COUNT function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.

This Post Has One Comment

Leave a Reply