SQLite cheat sheet

What's special about SQLite, on one printable page: how its flexible types behave, how to work with dates stored as text, its text functions, upserts, and the features you'll miss coming from PostgreSQL. Every snippet was run on SQLite.

Practise in 5 minutes
sqlstreakSQLite cheat sheetSQLite · tested on every release · sqlstreak.com

Types: flexible by default

SELECT name, typeof(age), typeof(adopted),
       typeof(arrived_on)
FROM pets LIMIT 1;

Storage classes: integer, real, text, blob, null. BOOLEAN is stored as 0/1, DATE as text.

CREATE TABLE t (n INTEGER);
INSERT INTO t VALUES ('ten');
SELECT n, typeof(n) FROM t;

A column type is a hint: 'ten' is stored as text.

CREATE TABLE t (n INTEGER) STRICT;
INSERT INTO t VALUES ('ten');
SELECT * FROM t;

STRICT tables reject the wrong type.

SELECT CAST('42abc' AS INTEGER),
       CAST(3.99 AS INTEGER);

CAST reads leading digits (42) and truncates (3).

Dates are text

SELECT date('2026-03-15', '+7 days');

Modifiers: '+N days', '+N months', '-1 year'…

SELECT date(arrived_on, 'start of month')
FROM pets LIMIT 3;

'start of month', 'start of year', 'weekday 1' (next Monday).

SELECT strftime('%Y-%m', arrived_on),
       strftime('%w', arrived_on)
FROM pets LIMIT 3;

Format: %Y %m %d %H %M, %w = weekday (0 = Sunday), %j = day of year.

SELECT julianday('2026-03-15')
     - julianday('2026-03-01');

Days between two dates (14).

SELECT date('now'), datetime('now');

Today, and now, in UTC.

Text

SELECT name || ' the ' || species FROM pets;

|| joins text.

SELECT substr(name, 1, 3), instr(name, 'o')
FROM pets;

Positions count from 1; instr gives 0 when not found.

SELECT name FROM pets WHERE name LIKE 'b%';

LIKE ignores case for English letters in SQLite.

SELECT name FROM pets WHERE name GLOB 'B*';

GLOB: case-sensitive, * and ? wildcards.

SELECT printf('%.2f kg', weight_kg) FROM pets;

printf (or format): C-style formats.

SELECT group_concat(name, ', ') FROM pets;

Join rows into one string (string_agg works too, since 3.44).

Insert, upsert, returning

INSERT OR IGNORE INTO pets (id, name, species, age, weight_kg, adopted, arrived_on)
VALUES (1, 'Dup', 'dog', 1, 5, 0, '2026-01-01');
SELECT COUNT(*) FROM pets;

Skip rows that would break a constraint.

INSERT INTO pets (id, name, species, age, weight_kg, adopted, arrived_on)
VALUES (3, 'Pickles', 'dog', 7, 23.0, false, '2025-09-21')
ON CONFLICT (id) DO UPDATE SET weight_kg = excluded.weight_kg;
SELECT weight_kg FROM pets WHERE id = 3;

Upsert: excluded = the row you tried to insert.

DELETE FROM pets WHERE species = 'bird'
RETURNING name;

RETURNING shows the affected rows (since 3.35).

Booleans and NULL

SELECT true, false, SUM(adopted) FROM pets;

true is 1, false is 0, so you can SUM a boolean.

SELECT name FROM pets WHERE breed IS NULL;

IS NULL, never = NULL.

SELECT IFNULL(breed, 'mixed'),
       iif(age >= 8, 'senior', 'young')
FROM pets;

IFNULL(a, b) and iif(cond, a, b): SQLite shortcuts.

SELECT NULL IS NULL, 1 IS NOT 2;

IS / IS NOT compare null-safely.

What SQLite doesn't have

SELECT 1::text;

No :: casts. Use CAST(x AS TEXT).

SELECT name FROM pets WHERE name ILIKE 'b%';

No ILIKE (LIKE already ignores case).

TRUNCATE pets;

No TRUNCATE: DELETE FROM pets;

SELECT DISTINCT ON (species) name FROM pets;

No DISTINCT ON: use ROW_NUMBER() = 1.

SELECT * FROM generate_series(1, 3);

No generate_series here: use WITH RECURSIVE.

PRAGMA table_info(pets);

Describe a table's columns.

More cheat sheets

Learn it properly

Read the PostgreSQL vs SQLite: SQL syntax differences guide, or practise in “Working with dates” (Pro): short exercises with instant feedback.