Function Reference

COUNT

Counts rows or non-NULL values of an expression.

MySQL 8.1
COUNT(* | [DISTINCT] expr)
  • [ ]optional part
  • a | bone of the options
*
Count all rows, including rows with NULL
Keyword
DISTINCT
Count only unique values of expr
KeywordOptional
expr
Expression, only rows where it is not NULL are counted

Return value

An integer (BIGINT); 0 for an empty set, not NULL

Examples

All rows in the table

MySQL 8.1
SELECT COUNT(*)
FROM FamilyMembers;
COUNT(*)
8

Number of distinct statuses

MySQL 8.1
SELECT COUNT(DISTINCT status)
FROM FamilyMembers;
COUNT(DISTINCT status)
4

All rows and non-NULL values

MySQL 8.1
SELECT COUNT(*) AS all_teachers,
	COUNT(middle_name) AS with_middle_name
FROM Teacher;
all_teacherswith_middle_name
1513

Details

COUNT(*) and COUNT(1) work the same and count all rows. COUNT(expr) skips NULL but counts an empty string. In MySQL, COUNT(DISTINCT a, b) takes several expressions and counts unique combinations.

See also

Where to learn