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 AVG

SQLite AVG

Summary: in this tutorial, you will learn how to use the SQLite AVG function to calculate the average value of a set of values.

Introduction to SQLite AVG function

The AVG function is an aggregate function that calculates the average value of all non-NULL values within a group.

The following illustrates the syntax of the AVG function:

AVG([ALL | DISTINCT] expression);

By default, the AVG function uses ALL clause whether you specify it or not. It means the AVG function will take all non-NULL values when it calculates the average value.

In case you want to calculate the average value of distinct (or unique) values, you need to specify the DISTINCT clause explicitly in expression.

If a column stores mixed data types such as integer, real, BLOB, and text, SQLite AVG function interprets the BLOB that does not look like a number as zero (0).

The value of the AVG function is always a floating point value or a NULL value. The AVG function only returns a NULL value if and only if all values in the group are NULL values.

You can take a quick test to see how the SQLite function works with various data types.

First, create a new table named avg_tests using the following statement:

CREATE TABLE avg_tests (val);

Try It

Next, insert some mixed values into the avg_tests table.

INSERT INTO avg_tests (val) VALUES (1), (2), (10.1), (20.5), ('8'), ('B'), (NULL), (x'0010'), (x'0011');

Try It

Then, query data from the avg_tests table.

SELECT rowid, val FROM avg_tests;

Try It

SQLite AVG function table example

After that, you can use the AVG function to calculate the average of the first four rows that contain only numeric values.

SELECT avg(val) FROM avg_tests WHERE rowid < 5;

Try It

SQLite AVG numeric values example

Finally, apply the AVG function to all the values in the val column of the avg_tests table.

SELECT avg(val) FROM avg_tests;

Try It

SQLite AVG function example

You have 9 rows in the avg_tests table. The row 7 is NULL. Therefore, when calculating the average, the AVG function ignores it and takes 8 rows into the calculation.

The first four rows are the integer and real values: 1,2, 10.1, and 20.5. The SQLite AVG function uses those values in the calculation.

The 5th and 6th row are text type because we inserted the as ‘B’ and ‘8’. Because 8 looks like a number, therefore SQLite interprets B as 0 and ‘8’ as 8.

The 8th and 9th rows are BLOB types that do not look like numbers, therefore, SQLite interprets these values as 0.

The AVG(cal) expression uses the following formula:

AVG(val) = (1 + 2 + 10.1 + 20.5 + 8 + 0 + 0 + 0 )/ 8 = 5.2

Let’s see how the DISTINCT clause works.

First, insert a new row into the avg_tests table with a value already exists.

INSERT INTO avg_tests (val) VALUES (10.1);

Try It

Second, apply the AVG function without DISTINCT clause:

SELECT avg(val) FROM avg_tests;

Try It

SQLite AVG ALL

Third, add the DISTINCT clause to the AVG function:

SELECT avg(DISTINCT val) FROM avg_tests;

Try It

SQLite AVG DISTINCT example

Because the avg_tests table has two rows with the same value 10.1, the AVG(DISTINCT) takes only the one row for calculation. Therefore, you got a different result.

SQLite AVG function practical examples

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

To calculate the average length of all tracks in milliseconds, you use the following statement:

SELECT avg(milliseconds) FROM tracks;

Try It

SQLite Select AVG example

SQLite AVG function with GROUP BY clause

To calculate the average length of tracks for every album, you use the AVG function with the GROUP BY clause.

First, the GROUP BY clause groups a set of tracks by albums. Then, the AVG function calculates the average length of tracks for each album.

See the following statement.

SELECT albumid, avg(milliseconds) FROM tracks GROUP BY albumid;

Try It

SQLite AVG function with INNER JOIN clause example

To get the album title together with the albumid column, you use the INNER JOIN clause in the above statement like the following query:

SELECT tracks.AlbumId, Title, round(avg(Milliseconds), 2) avg_length FROM tracks INNER JOIN albums ON albums.AlbumId = tracks.albumid GROUP BY tracks.albumid;

Try It

SQLite AVG with INNER JOIN

Notice that we used the ROUND function to round the floating value to 2 digits to the right of the decimal point.

SQLite AVG function with HAVING clause example

You can use either the AVG function or its column’s alias in the HAVING clause to filter groups. The following statement only gets the albums whose average length are between 100000 and 200000.

SELECT tracks.albumid, title, round(avg(milliseconds),2) avg_leng FROM tracks INNER JOIN albums ON albums.AlbumId = tracks.albumid GROUP BY tracks.albumid HAVING avg_leng BETWEEN 100000 AND 200000;

Try It

SQLite AVG function in HAVING clause

In this tutorial, we have shown you how to use the SQLite AVG function to calculate the average values of non-NULL values in a group.

  • Was this tutorial helpful ?
  • YesNo
Next SQLite COUNT

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.