Back to all posts

SQLite Functions Explained with Examples

For SQLite users who write simple SELECT statements and want SQLite to compute totals, text and dates inside the query.

On this page

A SQLite function computes a value inside a query, such as a total, an average, or a name in capitals. For example, AVG returns the average score of a students table, so your program reads one number instead of every score.

Every function call has the same shape:

SELECT function_name(argument, ...)
FROM table_name;
  • function_name is a built-in function, such as UPPER or AVG. Names are not case-sensitive.
  • argument is a column, a fixed value such as 90, or an expression. Some functions take none, such as RANDOM().

A scalar function, such as UPPER, returns one value for each row. An aggregate function, such as AVG, returns one value for a group of rows, such as all the rows of a table.

UPPER turns the four Students rows into four names in capitals, while AVG turns the same four rows into the single value 90.5

SQLite's built-in functions come in these families:

FamilyKindFunctions
AggregateaggregateCOUNT, SUM, TOTAL, AVG, MIN, MAX, GROUP_CONCAT
StringscalarUPPER, LOWER, LENGTH, SUBSTR, REPLACE, TRIM, INSTR
NumericscalarABS, ROUND, RANDOM
Date and timescalardate, time, datetime, julianday, unixepoch, strftime, timediff
NULL handlingscalarCOALESCE, IFNULL, NULLIF, IIF
Type and versionscalarTYPEOF, SQLITE_VERSION
WindowwindowROW_NUMBER, RANK, LAG, and every aggregate with OVER

The window functions tutorial covers the last family.

Every statement in this article was run on SQLite 3.50.4. Open a database file in sqlite3, SQLite's command-line program, or create one first:

sqlite3 testDB.db

Create a sample table of four students:

CREATE TABLE Students (
    name   TEXT NOT NULL,
    score  INTEGER NOT NULL,
    joined TEXT NOT NULL
);

INSERT INTO Students (name, score, joined) VALUES
    ('Adam',    85, '2026-01-15'),
    ('Bob',     95, '2026-02-01'),
    ('Charlie', 90, '2026-02-20'),
    ('David',   92, '2026-03-05');

COUNT, SUM, AVG, MIN and MAX

COUNT returns the number of rows, SUM the total, and AVG the average. MIN and MAX return the lowest and the highest value. For example, you can get all five figures for the students' scores in one row.

SELECT COUNT(*), SUM(score), AVG(score), MIN(score), MAX(score)
FROM Students;
COUNT(*)SUM(score)AVG(score)MIN(score)MAX(score)
436290.58595

COUNT(*) counts the four rows, and SUM returns 362, the four scores added up. MIN and MAX return Adam's 85 and Bob's 95.

AVG always returns a floating-point number, "even if all inputs are integers", as the aggregate function reference puts it. With GROUP BY, each function returns one value per group, as the SQL aggregate functions tutorial shows.

GROUP_CONCAT

GROUP_CONCAT joins the values of a column into one string. For example, you can list every student's name in one row.

SELECT GROUP_CONCAT(name, ', ')
FROM Students;
GROUP_CONCAT(name, ', ')
Adam, Bob, Charlie, David

The second argument goes between the names. Without it, GROUP_CONCAT puts a plain comma, with no space, between them.

The order of the names is arbitrary. To set the order, add ORDER BY after the last argument: GROUP_CONCAT(name, ', ' ORDER BY score DESC) returns Bob, David, Charlie, Adam. This form needs SQLite 3.44.0 or later, and SELECT sqlite_version(); returns the version you have.

UPPER, LOWER and LENGTH

UPPER returns a string in capital letters, and LOWER returns it in small letters. LENGTH returns the number of characters in a string. For example, you can show each name in capitals beside its length.

SELECT name, UPPER(name), LOWER(name), LENGTH(name)
FROM Students;
nameUPPER(name)LOWER(name)LENGTH(name)
AdamADAMadam4
BobBOBbob3
CharlieCHARLIEcharlie7
DavidDAVIDdavid5

Each of the four rows gets its own values, because these are scalar functions. Charlie has seven letters, so LENGTH returns 7.

date

The date function returns a date as text in the form YYYY-MM-DD. For example, you can show the day 30 days after each student joined.

SELECT name, joined, date(joined, '+30 days') AS plus_30_days
FROM Students;
namejoinedplus_30_days
Adam2026-01-152026-02-14
Bob2026-02-012026-03-03
Charlie2026-02-202026-03-22
David2026-03-052026-04-04

The second argument, '+30 days', is a modifier: a text argument that changes the date before the function returns it. Bob joined on 1 February, and 30 days later is 3 March, because February 2026 has 28 days.

SQLite has no date type, so joined is a TEXT column. The date and time functions read a moment stored as text, as a Julian day number (days since 4714 BC), or as a Unix timestamp (seconds since 1970):

The moment 2026-09-10 14:30:00 as ISO-8601 text, as the Julian day number 2461294.10416667 and as the Unix timestamp 1789050600, each read back by datetime() as the same moment

The datetime function returns a date with its time. datetime('now') returns the current moment in UTC, and datetime('now', 'localtime') returns the time on your computer's clock.

strftime

The strftime function writes a date in a format you give it. For example, you can write each joined date with the day first.

SELECT name, strftime('%d/%m/%Y', joined) AS day_first
FROM Students;
nameday_first
Adam15/01/2026
Bob01/02/2026
Charlie20/02/2026
David05/03/2026

SQLite replaces %d, %m and %Y with the day, the month and the year, so Adam's 2026-01-15 becomes 15/01/2026.

What goes wrong

SUM returns NULL when no row matches

TOTAL adds up a column like SUM, and always returns a floating-point number. SUM and TOTAL differ when no row matches:

SELECT SUM(score), TOTAL(score), AVG(score), COUNT(score)
FROM Students
WHERE name = 'Nobody';
SUM(score)TOTAL(score)AVG(score)COUNT(score)
NULL0.0NULL0

SUM and AVG return NULL, and TOTAL returns 0.0. If a sum of integers is too large for a 64-bit integer, SUM stops with the error "integer overflow". TOTAL returns a floating-point number instead.

MAX with two arguments

The core function reference says that max() "is a simple function when it has 2 or more arguments". With one argument, MAX(score) returns the highest score of the table. MAX(score, 90) compares two values on each row:

SELECT name, score, MAX(score, 90)
FROM Students;
namescoreMAX(score, 90)
Adam8590
Bob9595
Charlie9090
David9292

Adam's 85 is raised to 90, and the other scores stay as they are. MIN works the same way.

UPPER changes only the letters A to Z

UPPER and LOWER change only the letters A to Z:

SELECT UPPER('élan'), LOWER('ÉLAN'), LENGTH('élan');
UPPER('élan')LOWER('ÉLAN')LENGTH('élan')
éLANÉlan4

The é and the É keep their case. The core function reference says that lower() "works for ASCII characters only", and points to the ICU extension, which you load separately, for other letters. LENGTH counts é as one character.

No CREATE FUNCTION

SQLite has no CREATE FUNCTION statement and no stored functions. A program adds its own functions through its SQLite library, such as create_function in Python's sqlite3 module:

import sqlite3

first = sqlite3.connect("testDB.db")
first.create_function("grade", 1, lambda score: "A" if score >= 90 else "B")
print(first.execute("SELECT name, grade(score) FROM Students").fetchall())

second = sqlite3.connect("testDB.db")
print(second.execute("SELECT name, grade(score) FROM Students").fetchall())

The first connection prints the grades. The second connection stops with an error:

[('Adam', 'B'), ('Bob', 'A'), ('Charlie', 'A'), ('David', 'A')]
sqlite3.OperationalError: no such function: grade

The application-defined functions page says that custom functions "are created separately for each database connection". A view that calls grade fails with the same error in the sqlite3 shell and in every other program.

Text in a numeric column

A column declared INTEGER still accepts text. Insert a row for Eve with the score '88' as text, and a row for Frank with the score 'absent'. Then check what TYPEOF reports:

INSERT INTO Students (name, score, joined) VALUES
    ('Eve',   '88',     '2026-03-10'),
    ('Frank', 'absent', '2026-03-12');

SELECT name, score, TYPEOF(score)
FROM Students;
namescoreTYPEOF(score)
Adam85integer
Bob95integer
Charlie90integer
David92integer
Eve88integer
Frankabsenttext

SQLite stored Eve's '88' as the integer 88, because the text reads as a number. Frank's 'absent' stayed text. AVG, SUM and TOTAL read such text as 0:

SELECT AVG(score), SUM(score), TOTAL(score)
FROM Students;
AVG(score)SUM(score)TOTAL(score)
75.0450.0450.0

The average of the five numeric scores is 90. Frank's 0 pulls the average of all six rows down to 75.0. SUM returns a floating-point number as soon as one value is text.

To find such rows, filter with WHERE TYPEOF(score) <> 'integer'.

Run the queries in DbSchema

The SQL Editor in DbSchema runs a query and shows its rows in a grid under the editor. Connect DbSchema to the testDB.db file, and DbSchema downloads the SQLite driver on the first connection. Then press Editor in the toolbar, choose SQL Editor, paste a query from this article, and press Run.

The aggregate query in the DbSchema SQL Editor, with COUNT, SUM, AVG, MIN and MAX of the four student scores in the result grid

Download DbSchema at https://dbschema.com/download.html and connect it to SQLite. Paste the CREATE TABLE and INSERT statements into the SQL Editor, then run each query and change it. The free Community Edition includes the database connection and the SQL Editor.

DbSchema ER diagram designer DbSchema ER diagram designer

Design and visualize
your database schema

Edit referenced records
in related tables

Query your data
visually too

Reuse the SQL
generated

Free Download

Sources

  1. SQLite documentation: Built-in scalar SQL functions
  2. SQLite documentation: Built-in aggregate functions
  3. SQLite documentation: Date and time functions
  4. SQLite documentation: Datatypes in SQLite
  5. SQLite documentation: Application-defined SQL functions
  6. SQLite release 3.44.0