Home Sql Mysql-example MySQL Count



MySQL Count
Posted on: January 20, 2009 at 12:00 AM
This example illustrates how to use count() in MySQL database.

MySQL Count

     

This example illustrates how to use count() in MySQL database.

Count(): The Count() is an aggregate function that counts all the rows in a specified table. It returns a numeric value. If you use COUNT(*) that counts the number of rows.

 

 

 

 

Table: employee

CREATE TABLE `employee` ( 
`emp_id` int(11) NOT NULL auto_increment, 
`emp_name` varchar(10) character set utf8 NOT NULL, 
`emp_salary` int(11) NOT NULL, 
`emp_startDate` datetime NOT NULL, 
`dep_name` varchar(50) NOT NULL, 
PRIMARY KEY (`emp_id`) 
)

To select all records use the query: 

SELECT * FROM employee;

Output:

To use COUNT(*) with the sql query:

SELECT COUNT(*) FROM employee;

Output:

To use COUNT(column_name) with the GROUP BY sql query:

You can use count function on column. If it is used with group by clause then it counts all the items that are in a group. For example, the query below shows the department name and total no of employee in the department with the help of count function.

SELECT dep_name,COUNT(emp_name) As Total FROM employee GROUP BY (dep_name);

Related Tags for MySQL Count:
sqlmysqlcdatabasedatacountthistabexampletobaseexameiluseinasmnttresasehowrateratesxaxampsatmyisllmplstrssthstabpleplo


More Tutorials from this section

Ask Questions?    Discuss: MySQL Count  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.