Mysql Count Function

MySQL: COUNT Function

This tutorial explains how to use the MySQL COUNT function with syntax and examples.

Description

The MySQL COUNT function returns the count of an expression.

Syntax

The syntax for the COUNT function in MySQL is:

SELECT COUNT(aggregate_expression)
FROM tables
[WHERE conditions];

OR the syntax for the COUNT function when grouping the results by one or more columns is:

SELECT expression1, expression2, ... expression_n,
       COUNT(aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n;

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.

aggregate_expression

This is the column or expression whose non-null values will be counted.

tables

The tables that you wish to retrieve records from. There must be at least one table listed in the FROM clause.

WHERE conditions

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:

supplier_id supplier_name state
1 IBM CA
2 Microsoft
</td>
</tr>
<tr>
<td>3</td>
<td>NVIDIA</td>
<td>{% highlight java %}{% raw %}</td>
</tr>
</tbody>
</table>
</div>
<p>And if you ran the following SELECT statement that uses the COUNT function:</p>
{% highlight sql %}{% raw %}SELECT COUNT(supplier_id)
FROM suppliers;

Output: 3

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:

SELECT COUNT(state) 
FROM suppliers;

Output: 1

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.

Applies To

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.

SELECT COUNT(*) AS "Number of employees"
FROM employees
WHERE salary > 75000;

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.

SELECT COUNT(DISTINCT department) AS "Unique departments"
FROM employees
WHERE salary > 55000;

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'.

SELECT department, COUNT(*) AS "Number of employees"
FROM employees
WHERE state = 'CA'
GROUP BY department;

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.