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_nameis a built-in function, such as UPPER or AVG. Names are not case-sensitive.argumentis a column, a fixed value such as90, 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.
SQLite's built-in functions come in these families:
| Family | Kind | Functions |
|---|---|---|
| Aggregate | aggregate | COUNT, SUM, TOTAL, AVG, MIN, MAX, GROUP_CONCAT |
| String | scalar | UPPER, LOWER, LENGTH, SUBSTR, REPLACE, TRIM, INSTR |
| Numeric | scalar | ABS, ROUND, RANDOM |
| Date and time | scalar | date, time, datetime, julianday, unixepoch, strftime, timediff |
| NULL handling | scalar | COALESCE, IFNULL, NULLIF, IIF |
| Type and version | scalar | TYPEOF, SQLITE_VERSION |
| Window | window | ROW_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) |
|---|---|---|---|---|
| 4 | 362 | 90.5 | 85 | 95 |
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;
| name | UPPER(name) | LOWER(name) | LENGTH(name) |
|---|---|---|---|
| Adam | ADAM | adam | 4 |
| Bob | BOB | bob | 3 |
| Charlie | CHARLIE | charlie | 7 |
| David | DAVID | david | 5 |
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;
| name | joined | plus_30_days |
|---|---|---|
| Adam | 2026-01-15 | 2026-02-14 |
| Bob | 2026-02-01 | 2026-03-03 |
| Charlie | 2026-02-20 | 2026-03-22 |
| David | 2026-03-05 | 2026-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 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;
| name | day_first |
|---|---|
| Adam | 15/01/2026 |
| Bob | 01/02/2026 |
| Charlie | 20/02/2026 |
| David | 05/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) |
|---|---|---|---|
| NULL | 0.0 | NULL | 0 |
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;
| name | score | MAX(score, 90) |
|---|---|---|
| Adam | 85 | 90 |
| Bob | 95 | 95 |
| Charlie | 90 | 90 |
| David | 92 | 92 |
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 | Élan | 4 |
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;
| name | score | TYPEOF(score) |
|---|---|---|
| Adam | 85 | integer |
| Bob | 95 | integer |
| Charlie | 90 | integer |
| David | 92 | integer |
| Eve | 88 | integer |
| Frank | absent | text |
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.0 | 450.0 | 450.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.
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.

