Sql Count


Use Sql Count to count the number of rows of a table.

The syntax:

SELECT COUNT(column_name)
FROM   table_name 

The above statement counts non NULL value of the column_name

That could cause some confusion if you happened to have NULL values in the column_name as the total does not match the total number of rows. If you are interested in counting the number of records regardless of the values, use COUNT(*) instead.

The Sql syntax:

SELECT COUNT(*)
FROM   table_name 

That differences can also be useful when you want to find out if there is any NULL value in a certain field. So,simply compare the two results, when the total is different then you know there is Null value in that column. You can put the two COUNT keywords both in the Select statement, as follows:

SQL statement:

SELECT COUNT(column_name) AS total_col1,
       COUNT(*)           AS total
FROM   contacts 

There is also a time when you only want to know the COUNT of unique values of a field. To do this you need to add DISTINCT keyword into the Sql Count. Different with Sql Distinct with which you list the values, here you only get the count.

The syntax:

SELECT COUNT(DISTINCT column_name)
FROM   table_name 

Use the CONTACTS table once again.
You are going to implement the COUNT.

CPIdSurnameFirstnameAddressSuburbStatePostcode
1 White Alan 210 George St Sydney NSW 2000
2 Johnson Benjamin 11 Cooper St Surry Hill NSW 2010
3 Brown Peter 72 Haymarket St Haymarket NSW 2000
4 Anderson Thomas 33 Collins St Melbourne VIC 3000
5 Gomdas Tony 78 Marine Pde Coolangatta QLD 4225

SQL statement:

SELECT COUNT(state) AS total
FROM   contacts 

The result:

Total
5

The result would have been the same if you use COUNT(*), because there is no NULL value in State field.

Quick link. From here, you can learn the following:

Sql Select
The core of Sql, selecting data from table.

Sql Where
Conditionally selecting rows from table.

Sql Distinct
Eliminating duplicate values in the output.

Sql Sum
Getting the sum of a field

Sql Max
This function will return the highest value.

Sql Min
This function will return the lowest value.

Sql Avg
This function will return the average value.

Sql Order By
Sorting the output

Sql And Or
Connecting two or more conditions in the Where or Having clause.

Sql Group By
Grouping rows into sets.

Sql Having
To filter the result of aggregation

Sql Like
Pattern searching for conditions in Where or Having clause

Sql Between
Added to conditions to show results between two values.

Journey
Buy at AllPosters.com
A bend in the road is not the end of the road... unless you fail to make the turn

Positive Resources

All of us have at least two or three things that excite us. We could talk about these subjects for hours. Good news!. Did you know that Site Built It! can turn this type of deep interest and enthusiasm into a revenue-generating business?

Review some of the Site Build It! case studies and you'll absolutely discover something that ignites more positive ideas into your mind.

SBI! Video Tour


Invest the most
important 30 minutes
you will spend online.

Click, sit back, and
understand why SBI!
makes you succeed.