SQL COUNT() Function

The COUNT() function returns the number of rows that matches a specified criteria.

Syntax for SQL COUNT(column)

The COUNT(columnname) function returns the total   number of Record of the specified column.In this case the NULL values will not be counted.
SELECT COUNT(columnname) FROM tablename

Syntax for SQL  SQL COUNT(*) 

The COUNT(*) function returns the total number of records in the specified table:
SELECT COUNT(*) FROM tablename

Syntax for SQL   COUNT(DISTINCT columnname) 

The COUNT(DISTINCT columnname) function returns the number of distinct values of the specified column in a table:

SELECT COUNT(DISTINCT columnname) FROM tablename
Example: We have the following "Orders" table:
O_Id Date Price Customer_Name
1 2012/11/12 10000 Rajesh
2 2012/10/23 16000 Shiv
3 2012/09/02 7000 rupa
4 2012/09/03 3000 Hansen
5 2012/08/30 20000 Jensen
6 2012/10/04 1000 Shiv


Here we want to count the number of orders from 'Customer Shiv' the sql statement we use looks like this .

SELECT COUNT(Customer_name)  FROM Orders
WHERE Customer='Shiv'

Result :2

if we use this statement:

SELECT COUNT(*)  FROM Orders

Result:6
Tags: , , ,

Join Us!