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); |

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.
Ask Questions? Discuss: MySQL Count
Post your Comment