This tutorial explains how to use the MySQL COUNT function with syntax and examples.
The MySQL COUNT function returns the count of an expression.
The syntax for the COUNT function in MySQL is:
OR the syntax for the COUNT function when grouping the results by one or more columns is:
Parameters or Arguments
expression1, expression2, ... expression_n
Expressions that are not encapsulated within the COUNT function and must be included in the GROUP BY clause at the end of the SQL statement.
This is the column or expression whose non-null values will be counted.
The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.
Optional. These are conditions that must be met for the records to be selected.
Only includes NOT NULL Values
Not everyone realizes this, but the COUNT function will only include the records in the count where the value of expression in COUNT(expression) is NOT NULL. When expression contains a NULL value, it is not included in the COUNT calculations.
Let's look at a COUNT function example that demonstrates how NULL values are evaluated by the COUNT function.
For example, if you have the following table called suppliers:
This COUNT example will return 3 since all supplier_id values in the query's result set are NOT NULL.
However, if you ran the next SELECT statement that uses the COUNT function:
This COUNT example will only return 1, since only one state value in the query's result set is NOT NULL. That would be the first row where the state = 'CA'. It is the only row that is included in the COUNT function calculation.
The COUNT function can be used in the following versions of MySQL:
MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23
Example - With Single Expression
Let's look at some MySQL COUNT function examples and explore how to use the COUNT function in MySQL.
For example, you might wish to know how many employees have a salary above $75,000 / year.
In this COUNT function example, we've aliased the COUNT(*) expression as "Number of employees". As a result, "Number of employees" will display as the field name when the result set is returned.
Example - Using DISTINCT
You can use the DISTINCT clause within the COUNT function. For example, the SQL statement below returns the number of unique departments where at least one employee makes over $55,000 / year.
Again, the COUNT(DISTINCT department) field is aliased as "Unique departments". This is the field name that will display in the result set.
Example - Using GROUP BY
In some cases, you will be required to use the GROUP BY clause with the COUNT function.
For example, you could also use the COUNT function to return the name of the department and the number of employees (in the associated department) that are in the state of 'CA'.
Because you have listed one column in your SELECT statement that is not encapsulated in the COUNT function, you must use a GROUP BY clause. The department field must, therefore, be listed in the GROUP BY section.