totn SQLite Functions

SQLite: count Function

This SQLite tutorial explains how to use the SQLite count function with syntax and examples.

Description

The SQLite count function returns the count of an expression.

Syntax

The syntax for the count function in SQLite 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.

Applies To

The count function can be used in the following versions of SQLite:

  • SQLite 3.8.6, SQLite 3.8.x, SQLite 3.7.x, SQLite 3.6.x

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:

employee_id last_name first_name favorite_website
1 Smith Jane TechOnTheNet.com
2 Anderson Dave CheckYourMath.com
3 Johnson Sarah <Null>

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

SELECT count(employee_id)
FROM employees;

Result: 3

This count example will return 3 since all employee_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(favorite_website) 
FROM employees;

Result: 2

This count example will only return 2, since only two favorite_website values in the query's result set is NOT NULL. That would be the first record where the favorite_website is 'TechOnTheNet.com' and the second record where the favorite_website is 'CheckYourMath.com'. The third record in the table would not be include in the count function calculation because the favorite_website value is NULL.

Example - With Single Expression

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

For example, you might wish to know how many employees have a salary above $40,000 / year.

SELECT count(*) AS "Number of employees"
FROM employees
WHERE salary > 40000;

In this count function example, we've aliased the count(*) expression as "Number of employees".

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 $35,000 / year.

SELECT count(DISTINCT department) AS "Unique departments"
FROM employees
WHERE salary > 35000;

Again, the count(DISTINCT department) field is aliased as "Unique departments".

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 have 'TechOnTheNet.com' as their favorite_website.

SELECT department, count(*) AS "Number of employees"
FROM employees
WHERE favorite_website = 'TechOnTheNet.com'
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.