How to use COUNT in SQL

Usage of COUNT in SQL

In this article we will see the 'Usage of COUNT in SQL'

Usage Of Count In Sql


COUNT is an aggregate function in SQL Sever. It returns the number of records returned by a select query.

COUNT function works in SQL Server (starting with 2008 to latest version), Azure SQL Database and many other database also.

COUNT always returns an integer (INT) value as result.


Let's learn use of COUNT() function in SQL Server with an example 
(Example : How To Count the no. of members in distinct department)



Syntax of the COUNT() function: 

COUNT([ALL | DISTINCT  ] expression)

ALL  :  ALL is the default, and COUNT() function will applies to all values.

SELECT COUNT(*)  FROM [TABLENAME] 


DISTINCT  : COUNT() function will return the number of distinct (unique) non null values.

SELECT COUNT( DISTINCT [COLUMNAME] )  FROM [TABLENAME] 


EXPRESSION  : is an expression of any type. You cannot use a sub query or an aggregate function in the expression.

Post a Comment

0 Comments