SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Aggregate Functions / SQLite COUNT

SQLite COUNT

Summary: in this tutorial, you will learn how to use SQLite COUNT function to get the number of items in a group.

Introduction to SQLite COUNT() function

The function COUNT() is an aggregate function that returns the number of items in a group.

For example, you can use the COUNT() function to get the number of tracks from the tracks table, the number of artists from the artists table, playlists and the number of tracks in each, and so on.

The following illustrates the basic syntax of the COUNT function:

COUNT([ALL | DISTINCT] expression);
Code language: SQL (Structured Query Language) (sql)

Arguments

The function COUNT behaves according to the arguments that you pass in and the option ALL or DISTINCT that you specify.

The following describes the meanings of ALL and DISTINCT options:

  • ALL: when you specify all, the COUNT() function counts all non-null values include duplicates. The COUNT() function uses the ALL option by default if you skip it.
  • DISTINCT: if you explicitly use the DISTINCT option, the COUNT function counts only unique and non-null values.

The expression can be a column or an expression that involves columns to which the function COUNT() is applied.

SQLite provides another syntax of the COUNT() function:

COUNT(*)
Code language: SQL (Structured Query Language) (sql)

The COUNT(*) function returns the number of rows in a table, including the rows including NULL and duplicates.

SQLite COUNT() function illustration

First, create a table called t1 that has one column:

CREATE TABLE t1(c INTEGER);
Code language: SQL (Structured Query Language) (sql)

Second, insert five rows into the t1 table:

INSERT INTO t1(c) VALUES(1),(2),(3),(null),(3);
Code language: SQL (Structured Query Language) (sql)

Third, query data from the t1 table:

SELECT * FROM t1;
Code language: SQL (Structured Query Language) (sql)

Fourth, use the COUNT(*) function to return the number of rows in the t1 table:

SELECT COUNT(*) FROM t1;
Code language: SQL (Structured Query Language) (sql)

As you can see clearly from the output, the result set includes NULL and duplicate rows.

Fifth, use the COUNT(expression) to get the number of non-null values in the column c:

SELECT COUNT(c) FROM t1;
Code language: SQL (Structured Query Language) (sql)

In this example, the COUNT(c) returns the number of non-null values. It counts the duplicate rows as separate rows.

Sixth, use the COUNT(DISTINCT expression) to get the number of unique and non-null values in column c:

SELECT COUNT(DISTINCT c) FROM t1;
Code language: SQL (Structured Query Language) (sql)

SQLite COUNT(*) examples

We will take the table tracks in the sample database to demonstrate the functionality of the COUNT(*) function.

1) SQLite COUNT(*) example

To get the number of rows from the tracks table, you use the COUNT(*) function as follows:

SELECT count(*) FROM tracks;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite COUNT example

2) SQLite COUNT(*) with WHERE clause example

The following statement uses the COUNT(*) function with a WHERE clause to find the number of tracks whose album id is 10:

SELECT COUNT(*) FROM tracks WHERE albumid = 10;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite COUNT with WHERE clause

3) SQLite COUNT(*) with GROUP BY clause example

To get all the albums and the number of tracks in each album, you combine the COUNT(*) function with the GROUP BY clause:

SELECT albumid, COUNT(*) FROM tracks GROUP BY albumid;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite COUNT with GROUP BY

In this example:

  • First, the GROUP BY clause group tracks by album id.
  • Then, the COUNT(*) function returns the number of tracks for each album or group of tracks.

4) SQLite COUNT(*) with HAVING clause example

The following uses the COUNT(*) in the HAVING clause to find albums that have more than 25 tracks:

SELECT albumid, COUNT(*) FROM tracks GROUP BY albumid HAVING COUNT(*) > 25
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite COUNT with GROUP BY and HAVING clauses

5) SQLite COUNT(*) with INNER JOIN clause example

In order to make the output of the above query more useful, you can include the album’s name column. To do this, you add INNER JOIN and ORDER BY clauses to the query like the following query:

SELECT tracks.albumid, name, COUNT(*) FROM tracks INNER JOIN albums ON albums.albumid = tracks.albumid GROUP BY tracks.albumid HAVING COUNT(*) > 25 ORDER BY COUNT(*) DESC;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite COUNT complete example

SQLite COUNT(DISTINCT expression) examples

Let’s take a look at the  employees table from the sample database.

SELECT employeeid, lastname, firstname, title FROM employees;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite Employee Titles

To get the number of position titles, you pass the title column to the COUNT() function as follows:

SELECT COUNT(title) FROM employees;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite COUNT column example

However, to get the number of  unique titles, you need to add the DISTINCT option to the COUNT() function as shown in the following statement:

SELECT COUNT(DISTINCT title) FROM employees;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite COUNT DISTINCT example

In this tutorial, we have shown you how to use SQLite COUNT() function to count the number of items in a group.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite AVG
Next SQLite MAX

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK constraints
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2021 SQLite Tutorial. All Rights Reserved.