In this post explains how to use the COUNT function in SQL Server (Transact-SQL) with syntax and examples.
In SQL Server (Transact-SQL), the COUNT function returns the count of an expression.
The syntax for the COUNT function in SQL Server (Transact-SQL) is:
OR the syntax for the COUNT function when grouping the results by one or more columns is:
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.
This is the column or expression whose non-null values will be counted.
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
Optional. These are conditions that must be met for the records to be selected.
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_id | url | website_name |
---|---|---|
1 | www.AODBA.com | Tech On The Net |
2 | www.mySite.com |
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: 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 = 'Tech On The Net'. It is the only row that is included in the COUNT function calculation. Applies ToThe COUNT function can be used in the following versions of SQL Server (Transact-SQL):
Example - With Single FieldLet'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 'Mark'. 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 DISTINCTYou 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'. 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 BYIn 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'. 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. |