SQLite Syntax

Updated · 5 min read
mediumPostgreSQL 18sqlsyntaxreferencesqliteassessment

Every task from the syntax reference, written for SQLite, on one page. Use it as a quick lookup when you know what you want to do and need the SQLite spelling. Rows marked standard use SQL that other databases share. For the other databases, see the topic pages linked from the SQL Syntax by Database.

How these were checked: every PostgreSQL and SQLite form on this page was run (PostgreSQL 18 and SQLite 3.54). MySQL, SQL Server and Oracle forms follow each database's current documentation (MySQL 8.4, SQL Server 2022 and later, Oracle AI Database 26ai); version notes such as "8.0.19+" mark features that older releases lack.

Querying

TaskSQLite
First 10 rowsSELECT * FROM t ORDER BY id LIMIT 10;standard
Skip 20 rows, take 10SELECT * FROM t ORDER BY id LIMIT 10 OFFSET 20;standard
Case-insensitive matchSELECT * FROM t WHERE name LIKE 'nav%'; -- ignores ASCII casedialect
Rows with a missing valueSELECT * FROM t WHERE email IS NULL;standard
Groups with a conditionSELECT city, count(*) FROM t GROUP BY city HAVING count(*) > 1;standard
Full outer joinSELECT * FROM a FULL JOIN b ON b.id = a.id; -- 3.39+standard
Rows in one table but not anotherSELECT id FROM a EXCEPT SELECT id FROM b;standard
Quote a name with a spaceSELECT "first name" FROM t;standard

All five databases side by side: Syntax: Querying.

Changing Data

TaskSQLite
Insert several rowsINSERT INTO t (id, name) VALUES (1, 'a'), (2, 'b');standard
Get the new id backINSERT INTO t (name) VALUES ('a') RETURNING id; -- 3.35+dialect
Update from another tableUPDATE t SET name = s.name FROM s WHERE s.id = t.id; -- 3.33+dialect
Insert or update (upsert)INSERT INTO t (id, name) VALUES (1, 'a') ON CONFLICT (id) DO UPDATE SET name = excluded.name;dialect
Delete all rows quicklyDELETE FROM t;dialect
Copy rows into a new tableCREATE TABLE t2 AS SELECT * FROM t;dialect
Load a CSV file.import --csv --skip 1 file.csv tdialect

All five databases side by side: Syntax: Changing Data.

Tables and Keys

TaskSQLite
Auto-numbered primary keyid INTEGER PRIMARY KEYstandard
Money columnfee_usd NUMERIC -- stored as REAL or TEXT; use integer centsstandard
Text columnname TEXTstandard
True/false columnis_active INTEGER -- 0 or 1standard
Foreign key with cascadestudent_id INTEGER REFERENCES students (student_id) ON DELETE CASCADE -- PRAGMA foreign_keys = ONstandard
Check constraintCHECK (fee_usd > 0)standard
Create table if missingCREATE TABLE IF NOT EXISTS t (id INTEGER);dialect
Add a columnALTER TABLE t ADD COLUMN note TEXT;standard
Rename a columnALTER TABLE t RENAME COLUMN note TO memo;standard
Create an indexCREATE INDEX t_name_idx ON t (name);dialect
Create or replace a viewDROP VIEW IF EXISTS v; CREATE VIEW v AS SELECT id FROM t;dialect

All five databases side by side: Syntax: Tables and Keys.

Functions

TaskSQLite
Join textfirst_name || ' ' || last_namestandard
Text lengthlength(name)dialect
Part of a stringsubstr(name, 1, 3)standard
Replace NULLCOALESCE(email, 'none')standard
Round to centsround(x, 2)dialect
Today's datedate('now')standard
Add 30 daysdate(d, '+30 days')dialect
Days between datesjulianday(d2) - julianday(d1)dialect
Year of a datestrftime('%Y', d)standard
Format a datestrftime('%Y-%m-%d', d)dialect
Convert text to a numberCAST('450.00' AS REAL)standard
If / elseCASE WHEN x > 0 THEN 'yes' ELSE 'no' ENDstandard
Join values from many rowsgroup_concat(name, ', ' ORDER BY name) -- 3.44+standard

All five databases side by side: Syntax: Functions.

Advanced SQL

TaskSQLite
Common table expressionWITH x AS (SELECT 1 AS n) SELECT n FROM x;standard
Recursive CTEWITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n;standard
Rank rowsRANK() OVER (ORDER BY fee DESC) -- 3.25+standard
Running totalsum(x) OVER (ORDER BY d)standard
Conditional countcount(*) FILTER (WHERE method = 'card') -- 3.30+standard
SubtotalsNot supported: use UNION ALL of the groupingsstandard
Read a JSON field as textdata ->> '$.name' -- 3.38+dialect
Full-text searchbody MATCH 'join' -- on an FTS5 virtual tabledialect
A function languageNot supported in SQL: register functions from the host languagedialect

All five databases side by side: Syntax: Advanced SQL.

Administration

TaskSQLite
Start a transactionBEGIN;standard
SavepointSAVEPOINT s; ROLLBACK TO s;standard
Set the isolation levelAlways serializablestandard
Show a query planEXPLAIN QUERY PLAN SELECT ...;dialect
Create a loginNo users: access is the file's permissionsdialect
Grant read accessNo privileges in SQLstandard
Back up a database.backup db.backup -- in the sqlite3 shelldialect
Parameter placeholder in app code? or :namedialect
Server versionSELECT sqlite_version();dialect

All five databases side by side: Syntax: Administration.

Was this tutorial helpful?