COUNT() — Count Rows
The COUNT() function returns the number of rows that match a condition.
It is one of the most commonly used aggregate functions.
You can use COUNT() in the following two ways:
| Syntax | Syntax Example | Expected Output Description |
|---|---|---|
| COUNT(*) | SELECT COUNT(*) FROM employees; | Total number of rows in the table |
| COUNT(column) | SELECT COUNT(gender) FROM employees; | Number of rows where gender is not NULL |
Syntax
SELECT COUNT(*) FROM table_name;
or:
SELECT COUNT(column) FROM table_name;
Example 1: Total Number of Employees
SELECT COUNT(*) FROM employees;
This returns the total number of rows in the employees table (total employees).
Example 2: Count Employees per Department
SELECT department, COUNT(*)
FROM employees
GROUP BY department;
This returns the number of employees in each department.
Example 3: Count Non-NULL Gender Values
SELECT COUNT(gender) FROM employees;
This counts the number of rows where gender is NOT NULL.
Example 4: Count Employees in IT Department
SELECT COUNT(*) FROM employees
WHERE department = 'IT';
This counts the number of employees who work in the IT department.
Tip
COUNT(*)counts all rows.COUNT(column)counts non-NULL values only.
The COUNT() function is a simple but powerful way to explore how many rows match your query!