count() function in MYSQL by R4R Team

MYSQL COUNT() function:
-The count() function is used to return the count of an expression.
-When we want to count number of records in any column then we use count() function.

Syntax:
SELECT COUNT(expression)
FROM table_name
WHERE conditions;

Display all record of table:

SELECT * FROM employee;

emp_id emp_name department emp_salary
1 Aman D1 34000
2 Ram D2 43000
3 Ramesh D1 20000
4 John D3 65000
5 Sita D1 10000
6 Dinesh D2 15000

Now we count all the employee:

SELECT COUNT(emp_id) from employee;

COUNT(emp_id)
6


Now we count the number of the employee which belong to department D1:

SELECT department,COUNT(emp_id) from employee WHERE department=D1;

department COUNT(emp_id)
D13



Leave a Comment: