totn MariaDB Functions

MariaDB: COUNT Function

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

Description

The MariaDB COUNT function returns the count of an expression.

Syntax

The syntax for the COUNT function in MariaDB 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 sites:

site_id site_name server_name
1 TechOnTheNet.com MyServer
2 CheckYourMath.com  
3 BigActivities.com  

And if you ran the following SELECT statement that uses the COUNT function:

SELECT COUNT(site_id)
FROM sites;

Result: 3

This COUNT example will return 3 since all site_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(server_name) 
FROM sites;

Result: 1

This COUNT example will only return 1, since only one server_name value in the query's result set is NOT NULL. That would be the first row where the server_name is 'MyServer'. 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 MariaDB:

  • MariaDB 10

Example - With Single Expression

Let's look at some MariaDB COUNT function examples and explore how to use the COUNT function in MariaDB.

First, we'll go though an example of how to use the COUNT function with a single expression in MariaDB.

For example:

SELECT COUNT(*) AS "Number of Sites"
FROM sites
WHERE site_name in ('TechOnTheNet.com', 'CheckYourMath.com');

In this COUNT function example, we will calculate the number of sites where the site_name is 'TechOnTheNet.com' or 'CheckYourMath.com'. We've aliased the COUNT(*) expression as "Number of Sites". As a result, "Number of Sites" will display as the column title when the result set is returned.

Example - Using DISTINCT

Next, let's look at how to use the DISTINCT clause within the COUNT function in MariaDB.

For example:

SELECT COUNT(DISTINCT file_size) AS "Number of Unique Sizes"
FROM pages
WHERE site_name = 'TechOnTheNet.com';

In this COUNT example, if there were two file_size values that were the same, only one of these values would be used in the COUNT function calculation.

Example - Using GROUP BY

Finally, let's look at how to use the GROUP BY clause with the COUNT function in MariaDB.

If you are returning columns that are no encapsulated in the COUNT function, you must use the GROUP BY clause.

For example:

SELECT site_id, COUNT(*) AS "Number of pages per site"
FROM pages
WHERE site_name in ('TechOnTheNet.com', 'BigActivities.com')
GROUP BY site_id;

In this COUNT function example, we must use a GROUP BY clause because the site_id field is not encapsulated in the COUNT function. The site_id column must, herefore, be listed in the GROUP BY section at the end of the SQL statement.