Thursday, March 16, 2017

SQL SELECT COUNT()

In SQL Server, the COUNT() function returns the total number of rows in a select query. The COUNT() is used with SELECT to return the number of rows based on the conditions of the SELECT.

Syntax of SELECT COUNT():
           
      SELECT COUNT ({[[ALL|DISTINCT] expression] | *})
      FROM TABLE_NAME
      [WHERE Condition(s)]

[] = Optional code

ALL is default option and need not to be specified. The ALL keyword includes all the non-NULL values.

DISTINCT counts the distinct value of the column.

FROM specifies the location (TABLE_NAME i.e. database table) from where the data is retrieved.

WHERE CONDITION(S) specifies the search conditions or filters for the rows returned by the query.

The COUNT() returns 0 value if no matching rows are returned from the query.

Examples:

SELECT COUNT(*) FROM Person.Person;

It returns total number of rows in the table.




The ALL returns all the non-NULL values of the column.

SELECT COUNT(ALL Title) FROM Person.Person;



The COUNT (column_name) function returns the number of values except NULL values of the column specified in the SELECT.

SELECT COUNT(Title) FROM Person.Person;



The COUNT (DISTINCT column_name) function returns the number of unique values except NULL values of the column specified in the SELECT.

SELECT COUNT(DISTINCT Title) FROM Person.Person;



Share This
Previous Post
Next Post

TekGrabs, established in 2017, is in the business of sharing Information. It provides news, articles and photos about the different technologies, inventions in different domains. Provides information on programming and databases.

0 comments: