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 NULLKeyword
- DISTINCT
- Count only unique values of exprKeywordOptional
- 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;Number of distinct statuses
MySQL 8.1
SELECT COUNT(DISTINCT status)
FROM FamilyMembers;All rows and non-NULL values
MySQL 8.1
SELECT COUNT(*) AS all_teachers,
COUNT(middle_name) AS with_middle_name
FROM Teacher;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.