SQLite Tutorial

  • Home
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • Interfaces
    • SQLite Java
    • SQLite Node.js
    • SQLite PHP
    • SQLite Python
  • 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

SQLite 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, the number of artists, playlists and the number of tracks in each playlist, and so on.

The following illustrates the syntax of the COUNT function:

1
COUNT([ALL | DISTINCT] expression);

Arguments

The COUNT function behaves based on the arguments that you pass in. The following are arguments of the COUNT function:

  • ALL: the COUNT function is applied to all values in the group. The COUNT function uses ALL by default, therefore, you can omit it.
  • DISTINCT: the COUNT function only considers unique non-null values.
  • expression: is any expression that can be a column of a table.

SQLite COUNT(*) function

Another form of the COUNT function is as follows:

1
COUNT(*)

The COUNT(*) function returns the number of rows in a table, including the rows that contain NULL values. The COUNT(*) function counts each row individually. It takes no parameters other than asterisk symbol (*).

SQLite COUNT function examples

Let’s take few examples to see the COUNT function works.

SQLite COUNT(*) example

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

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

1
2
3
4
SELECT
count(*)
FROM
tracks;

Try It

SQLite COUNT example

You can add the WHERE clause to find the number of tracks in the album with id 10:

1
2
3
4
5
6
SELECT
count(*)
FROM
tracks
WHERE
albumid = 10;

Try It

SQLite COUNT with WHERE clause

To get albums and the number of tracks for each, you use the COUNT function with the GROUP BY clause as shown in the following query:

1
2
3
4
5
6
7
SELECT
albumid,
count(*)
FROM
tracks
GROUP BY
albumid;

Try It

SQLite COUNT with GROUP BY

If you want to find the albums that have more than 25 tracks, you use the HAVING clause:

1
2
3
4
5
6
7
8
SELECT
albumid,
count(*)
FROM
tracks
GROUP BY
albumid
HAVING count(*) > 25

Try It

SQLite COUNT with GROUP BY and HAVING clauses

The result set is not so informative. You need to add the album’s name and sort the album by the number of tracks.

To do this, you add the INNER JOIN and ORDER BY clauses to the query like the following query:

1
2
3
4
5
6
7
8
9
10
11
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;

Try It

SQLite COUNT complete example

SQLite COUNT(DISTINCT expression) examples

Let’s take a look at the  employees table.

employees table

1
2
3
4
5
6
7
SELECT
employeeid,
lastname,
firstname,
title
FROM
employees;

Try It

SQLite Employee Titles

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

1
2
3
4
SELECT
COUNT(title)
FROM
employees;

Try It

SQLite COUNT column example

However, to get the number of unique titles, you need to pass the DISTINCT clause to the COUNT function as the following statement:

1
2
3
4
SELECT
COUNT(DISTINCT title)
FROM
employees;

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 ?
  • Yes   No
Previous Tutorial: SQLite AVG
Next Tutorial: 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 GLOB
  • SQLite Left Join
  • SQLite Inner 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 Constraint
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Drop Table
  • SQLite Create 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 Window Frame
  • SQLite CUME_DIST
  • SQLite PERCENT_RANK
  • SQLite DENSE_RANK
  • SQLite NTILE
  • SQLite NTH_VALUE
  • SQLite LAST_VALUE
  • SQLite FIRST_VALUE

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2019 SQLite Tutorial. All rights Reserved.

⤒