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 SUM

SQLite SUM

Summary: in this tutorial, you will learn how to use the SQLite SUM function to calculate the sum of all values.

Introduction to SQLite SUM function

The SUM function is an aggregate function that returns the sum the non-NULL values or only the distinct values in a group.

The following expression illustrates the syntax of the SUM function:

SUM([ALL | DISTINCT] expression);

The SUM function uses ALL clause by default. It means that all the input values, whether duplicate or not, are considered when the SUM function performs the calculation.

If you want to calculate the sum of unique values, you have to specify the  DISTINCT clause explicitly in the expression.

The result of the SUM function is an integer if all input non-NULL values are integers. If any input value is neither an integer nor a NULL value, the result of the SUM function is a floating-point value.

The result of the SUM function is NULL if and only if all input values are NULL.

In case there is an integer overflow error happens and all input values are NULL or integers, the SUM function throws an integer overflow exception.

SQLite SUM function examples

We will use the tracks table in the sample database for the demonstration.

To get the total length of all tracks in the tracks table, you use the SUM function as the following statement:

SELECT SUM(milliseconds) FROM tracks;

Try It

SQLite SUM function example

SQLite SUM function with GROUP BY clause

To calculate the total length of each album, you need to use the SUM function with the  GROUP BY clause.

First, the GROUP BY clause groups a set of tracks by albums. Then, the SUM function calculates the sum of lengths of tracks per album.

The following statement illustrates the idea:

SELECT AlbumId, SUM(milliseconds) FROM tracks GROUP BY AlbumId;

Try It

SQLite SUM with GROUP BY clause example

SQLite SUM function and INNER JOIN clause example

To include the album titles in the output, you join the tracks table to the albums table using the  INNER JOIN clause as follows:

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

Try It

SQLite SUM with INNER JOIN clause example

SQLite SUM function and HAVING clause example

You can use the SUM function in the  HAVING clause to filter groups based on a specified condition.

For example, the following statement gets all albums whose total lengths are greater than 1,000,000 milliseconds:

SELECT tracks.albumid AlbumId, Title, SUM(milliseconds) FROM tracks INNER JOIN albums ON albums.albumid= tracks.albumid GROUP BY tracks.albumid, title HAVING SUM(milliseconds) > 1000000;

Try It

SQLite SUM with HAVING clause example

In this tutorial, we have introduced you to the SQLite SUM function that returns the sum of values in a group.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite MIN
Next SQLite GROUP_CONCAT

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.