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 Tutorial / SQLite Group By

SQLite Group By

Summary: in this tutorial, you will learn how to use SQLite GROUP BY clause to make a set of summary rows from a set of rows.

Introduction to SQLite GROUP BY clause

The GROUP BY clause is an optional clause of the SELECT statement. The GROUP BY clause a selected group of rows into summary rows by values of one or more columns.

The GROUP BY clause returns one row for each group. For each group, you can apply an aggregate function such as MIN, MAX, SUM, COUNT, or AVG to provide more information about each group.

The following statement illustrates the syntax of the SQLite GROUP BY clause.

SELECT column_1, aggregate_function(column_2) FROM table GROUP BY column_1, column_2;

Try It

The GROUP BY clause comes after the FROM clause of the SELECT statement. In case a statement contains a WHERE clause, the GROUP BY clause must come after the WHERE clause.

Following the GROUP BY clause is a column or a list of comma-separated columns used to specify the group.

SQLite GROUP BY examples

We use the tracks table from the sample database for the demonstration.

SQLite GROUP BY clause with COUNT function

The following statement returns the album id and the number of tracks per album. It uses the GROUP BY clause to groups tracks by album and applies the COUNT() function to each group.

SELECT albumid, COUNT(trackid) FROM tracks GROUP BY albumid;

Try It

SQLite GROUP BY with COUNT function

You can use the ORDER BY clause to sort the groups as follows:

SELECT albumid, COUNT(trackid) FROM tracks GROUP BY albumid ORDER BY COUNT(trackid) DESC;

Try It

SQLite GROUP BY with COUNT function and ORDER BY clause

SQLite GROUP BY and INNER JOIN clause

You can query data from multiple tables using the INNER JOIN clause, then use the GROUP BY clause to group rows into a set of summary rows.

For example, the following statement joins the tracks table with the albums table to get the album’s titles and uses the GROUP BY clause with the COUNT function to get the number of tracks per album.

SELECT tracks.albumid, title, COUNT(trackid) FROM tracks INNER JOIN albums ON albums.albumid = tracks.albumid GROUP BY tracks.albumid;

Try It

SQLite GROUP BY with INNER JOIN

SQLite GROUP BY with HAVING clause

To filter groups, you use the GROUP BY with HAVING clause. For example, to get the albums that have more than 15 tracks, you use the following statement:

SELECT tracks.albumid, title, COUNT(trackid) FROM tracks INNER JOIN albums ON albums.albumid = tracks.albumid GROUP BY tracks.albumid HAVING COUNT(trackid) > 15;

Try It

SQLite GROUP BY with HAVING clause

SQLite GROUP BY clause with SUM function example

You can use the SUM function to calculate total per group. For example, to get total length and bytes for each album, you use the SUM function to calculate total milliseconds and bytes.

SELECT albumid, SUM(milliseconds) length, SUM(bytes) size FROM tracks GROUP BY albumid;

Try It

SQLite GROUP BY with SUM function

SQLite GROUP BY with MAX, MIN, and AVG functions

The following statement returns the album id, album title, maximum length, minimum length, and the average length of tracks in the tracks table.

SELECT tracks.albumid, title, min(milliseconds), max(milliseconds), round(avg(milliseconds),2) FROM tracks INNER JOIN albums ON albums.albumid = tracks.albumid GROUP BY tracks.albumid;

Try It

SQLite GROUP BY with MAX MIN AVG functions

SQLite GROUP BY multiple columns example

In the previous example, we have used one column in the GROUP BY clause. SQLite allows you to group rows by multiple columns.

For example, to group tracks by media type and genre, you use the following statement:

SELECT MediaTypeId, GenreId, COUNT(TrackId) FROM tracks GROUP BY MediaTypeId, GenreId;

Try It

SQL GROUP BY multiple columns example

SQLite uses the combination of values of MediaTypeId and GenreId columns as a group e.g., (1,1) and (1,2). It then applies the COUNT function to return the number of tracks in each group.

SQLite GROUP BY date example

See the following invoices table from the sample database:

The following statement returns the number of invoice by years.

SELECT STRFTIME('%Y', InvoiceDate) InvoiceYear, COUNT(InvoiceId) InvoiceCount FROM invoices GROUP BY STRFTIME('%Y', InvoiceDate) ORDER BY InvoiceYear;

Here is the output:

SQLite GROUP BY date example

In this example:

  • The function STRFTIME('%Y', InvoiceDate) returns a year from a date string.
  • The GROUP BY clause groups the invoices by years.
  • The function COUNT() returns the number of invoice in each year (or group).

In this tutorial, you have learned how to use the SQLite GROUP BY clause to group rows into a set of summary rows.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Self-Join
Next SQLite Having

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 Constraint
  • 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 © 2020 SQLite Tutorial. All Rights Reserved.