Published on: 24 September, 2013

SQL COUNT()

Posted by andy | 1943 views 0 likes 0 favourites 0 comments
Tumblr Digg Google Plus

Example Customer Table

The following data table is for example only. Let's use COUNT() function to get the total rows/records from a customer table. The column we going to use are the Name and Age columns.

ID Name Age
1 Thomas Anderson 45
2 Andrew Golden 38
3 Ken Tom 30
4 Andrew Golden 36
5 Timothy Kirk 32
6 Kelly Smith NULL

Example of Running SQL Query

SELECT COUNT(Name) AS Total
FROM Customers

After running the above query, it will return the following result.

Total
6

Another example

Let's do another count to a column that contains a NULL value.

SELECT COUNT(Age) AS Total
FROM Customers

After running the above query, it will return the following result.

Total
5
Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.
Related Articles
Published on: 12 February, 2018

If you have a situation where you want to add a column into a table in SQL Server, it would be a good idea to perform a check first if a column has already existed in a table. Otherwise, if you try to add a duplicate column, you may get an error message when you try to add a column via SQL query.

Published on: 05 February, 2018
If you develop a website or a program that allows your users to perform a search. When the users perform a search query, you probably want to return the best match result first because it is actually more relevant to the search they need. For example, let says we have a database table contains the following list of product.