SQLite Syntax
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
| Task | SQLite | |
|---|---|---|
| First 10 rows | SELECT * FROM t ORDER BY id LIMIT 10; | standard |
| Skip 20 rows, take 10 | SELECT * FROM t ORDER BY id LIMIT 10 OFFSET 20; | standard |
| Case-insensitive match | SELECT * FROM t WHERE name LIKE 'nav%'; -- ignores ASCII case | dialect |
| Rows with a missing value | SELECT * FROM t WHERE email IS NULL; | standard |
| Groups with a condition | SELECT city, count(*) FROM t GROUP BY city HAVING count(*) > 1; | standard |
| Full outer join | SELECT * FROM a FULL JOIN b ON b.id = a.id; -- 3.39+ | standard |
| Rows in one table but not another | SELECT id FROM a EXCEPT SELECT id FROM b; | standard |
| Quote a name with a space | SELECT "first name" FROM t; | standard |
All five databases side by side: Syntax: Querying.
Changing Data
| Task | SQLite | |
|---|---|---|
| Insert several rows | INSERT INTO t (id, name) VALUES (1, 'a'), (2, 'b'); | standard |
| Get the new id back | INSERT INTO t (name) VALUES ('a') RETURNING id; -- 3.35+ | dialect |
| Update from another table | UPDATE 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 quickly | DELETE FROM t; | dialect |
| Copy rows into a new table | CREATE TABLE t2 AS SELECT * FROM t; | dialect |
| Load a CSV file | .import --csv --skip 1 file.csv t | dialect |
All five databases side by side: Syntax: Changing Data.
Tables and Keys
| Task | SQLite | |
|---|---|---|
| Auto-numbered primary key | id INTEGER PRIMARY KEY | standard |
| Money column | fee_usd NUMERIC -- stored as REAL or TEXT; use integer cents | standard |
| Text column | name TEXT | standard |
| True/false column | is_active INTEGER -- 0 or 1 | standard |
| Foreign key with cascade | student_id INTEGER REFERENCES students (student_id) ON DELETE CASCADE -- PRAGMA foreign_keys = ON | standard |
| Check constraint | CHECK (fee_usd > 0) | standard |
| Create table if missing | CREATE TABLE IF NOT EXISTS t (id INTEGER); | dialect |
| Add a column | ALTER TABLE t ADD COLUMN note TEXT; | standard |
| Rename a column | ALTER TABLE t RENAME COLUMN note TO memo; | standard |
| Create an index | CREATE INDEX t_name_idx ON t (name); | dialect |
| Create or replace a view | DROP VIEW IF EXISTS v; CREATE VIEW v AS SELECT id FROM t; | dialect |
All five databases side by side: Syntax: Tables and Keys.
Functions
| Task | SQLite | |
|---|---|---|
| Join text | first_name || ' ' || last_name | standard |
| Text length | length(name) | dialect |
| Part of a string | substr(name, 1, 3) | standard |
| Replace NULL | COALESCE(email, 'none') | standard |
| Round to cents | round(x, 2) | dialect |
| Today's date | date('now') | standard |
| Add 30 days | date(d, '+30 days') | dialect |
| Days between dates | julianday(d2) - julianday(d1) | dialect |
| Year of a date | strftime('%Y', d) | standard |
| Format a date | strftime('%Y-%m-%d', d) | dialect |
| Convert text to a number | CAST('450.00' AS REAL) | standard |
| If / else | CASE WHEN x > 0 THEN 'yes' ELSE 'no' END | standard |
| Join values from many rows | group_concat(name, ', ' ORDER BY name) -- 3.44+ | standard |
All five databases side by side: Syntax: Functions.
Advanced SQL
| Task | SQLite | |
|---|---|---|
| Common table expression | WITH x AS (SELECT 1 AS n) SELECT n FROM x; | standard |
| Recursive CTE | WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n WHERE x < 3) SELECT x FROM n; | standard |
| Rank rows | RANK() OVER (ORDER BY fee DESC) -- 3.25+ | standard |
| Running total | sum(x) OVER (ORDER BY d) | standard |
| Conditional count | count(*) FILTER (WHERE method = 'card') -- 3.30+ | standard |
| Subtotals | Not supported: use UNION ALL of the groupings | standard |
| Read a JSON field as text | data ->> '$.name' -- 3.38+ | dialect |
| Full-text search | body MATCH 'join' -- on an FTS5 virtual table | dialect |
| A function language | Not supported in SQL: register functions from the host language | dialect |
All five databases side by side: Syntax: Advanced SQL.
Administration
| Task | SQLite | |
|---|---|---|
| Start a transaction | BEGIN; | standard |
| Savepoint | SAVEPOINT s; ROLLBACK TO s; | standard |
| Set the isolation level | Always serializable | standard |
| Show a query plan | EXPLAIN QUERY PLAN SELECT ...; | dialect |
| Create a login | No users: access is the file's permissions | dialect |
| Grant read access | No privileges in SQL | standard |
| Back up a database | .backup db.backup -- in the sqlite3 shell | dialect |
| Parameter placeholder in app code | ? or :name | dialect |
| Server version | SELECT sqlite_version(); | dialect |
All five databases side by side: Syntax: Administration.
Was this tutorial helpful?