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.
|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;|
To use COUNT(*) with the sql query:
|SELECT COUNT(*) FROM employee;|
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);|
Posted on: January 20, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles