COUNT
An aggregate function that counts the number of rows in a table or the number of rows matching a condition. Often used with GROUP BY to count rows per group.
Syntax
Returns the total number of rows in the table (includes NULL).
SELECT COUNT(*) FROM table_name;
Returns the number of rows where the specified column is not NULL.
SELECT COUNT(column_name) FROM table_name;
Returns the number of distinct (non-duplicate) values in the specified column.
SELECT COUNT(DISTINCT column_name) FROM table_name;
Returns the number of rows that match the condition.
SELECT COUNT(*) FROM table_name WHERE condition;
Syntax List
| Syntax | Description |
|---|---|
| COUNT(*) | Counts all rows, including those with NULL values. This is the most common usage. |
| COUNT(column_name) | Counts only rows where the specified column is not NULL. Rows with NULL are excluded. |
| COUNT(DISTINCT column_name) | Counts the number of unique values in the specified column, excluding duplicates. |
Sample Code
The following 'employees' table is used in the examples below.
Returns the total number of employees.
sample_count.sql
SELECT COUNT(*) AS employee_count FROM employees;
+----------------+ | employee_count | +----------------+ | 5 | +----------------+ 1 row in set
Returns the number of employees who have a bonus set (excludes NULL).
sample_count.sql
SELECT COUNT(bonus) AS bonus_eligible_count FROM employees;
+----------------------+ | bonus_eligible_count | +----------------------+ | 3 | +----------------------+ 1 row in set
Returns the number of distinct departments.
sample_count.sql
SELECT COUNT(DISTINCT department) AS department_count FROM employees;
+------------------+ | department_count | +------------------+ | 5 | +------------------+ 1 row in set
Returns the number of employees in the Dojima Family.
sample_count.sql
SELECT COUNT(*) AS dojima_count FROM employees WHERE department = 'Dojima Family';
+--------------+ | dojima_count | +--------------+ | 1 | +--------------+ 1 row in set
Returns the number of employees per department (used with GROUP BY).
sample_count.sql
SELECT department, COUNT(*) AS headcount FROM employees GROUP BY department ORDER BY headcount DESC;
+--------------------+-----------+ | department | headcount | +--------------------+-----------+ | Dojima Family | 1 | | Majima Family | 1 | | Sky Finance | 1 | | Nishikiyama Family | 1 | | Saejima Family | 1 | +--------------------+-----------+ 5 rows in set
Database-Specific Notes
The COUNT(*), COUNT(column_name), and COUNT(DISTINCT column_name) syntaxes work the same way across all major databases.
SELECT COUNT(*) AS employee_count FROM employees; SELECT COUNT(DISTINCT department) AS department_count FROM employees; SELECT department, COUNT(*) AS headcount FROM employees GROUP BY department;
Notes
COUNT is an aggregate function. The key difference between COUNT(*) and COUNT(column_name) is how they handle NULL values. If a column contains NULL values, the two forms can return different results on the same table, so use the appropriate form with care.
When using COUNT(*) > 0 to check whether rows exist, an EXISTS subquery is faster in MySQL because it stops as soon as a matching row is found. For existence checks on large tables, consider using EXISTS instead.
For per-group aggregation, see GROUP BY / HAVING. For totals and averages, see SUM / AVG.
If you find any errors or copyright issues, please contact us.