Sunday, January 24, 2016

SQL COUNT *: Count The Number of Rows in a Certain Table

Functions are keywords in SQL used to manipulate values in database for output purposes. A function is a command normally used together with a column name that processes the data to produce an output. SQL contains several types of functions. SQL Aggregate functions can perform useful functions such as COUNT, SUM, AVG, MAX, MIN.

Another aggregate function other than SUM is COUNT. The purpose of COUNT function is to return the number of rows found in a table.

For example, if we want to count how many sales transaction are there in our SALES table found below, we will used the query below.


select COUNT(*) from sales;


The output is 3 since there are only 3 sales transaction in our SALES table.

The COUNT(*) on the above output is not quite readable. There will be cases wherein you need to use an alias to easily identify a certain column returned using a function. Or perhaps to access the returned output later in php. To  use an alias, use the as keyword followed by your alias. I provided an example below. The nooftrans is your now your new column name that contains the result of the COUNT function.

select COUNT(*) as nooftrans from sales;

Hope this helps! Feel free to post a comment below for questions. ^_^

SQL COUNT *: Count The Number of Rows in a Certain Table

No comments:

Post a Comment