SQLite Cheat Sheet: sqlite3 Commands and SQL Syntax (Tested)

A one-page reference for SQLite: the sqlite3 shell’s dot-commands, the SQL you write every day, and the PRAGMAs that matter. Each section links to a full tested guide.

Every command on this page was run on 6 October 2026 with SQLite 3.51.0, top to bottom in a single sqlite3 session, with no errors. The exported CSV, the import, the dump and the VACUUM INTO backup were then checked.

Download the printable PDF (A4, 2 pages, the same commands as this page).

sqlite3 shell

TaskCommand
Open or create a database file.open shop.db
Turn on column headers.headers on
Aligned column output.mode column
List tables.tables
Show a table’s CREATE statement.schema products

Export a query to CSV, then import it into a new table (the first row becomes the column names):

.mode csv
.output products.csv
SELECT * FROM products;
.output stdout
.import --csv products.csv products_copy

Back up the whole database as SQL text, and restore it into another database with .read:

.output dump.sql
.dump
.output stdout

-- later, in a new database:
.read dump.sql
TaskCommand
Run SQL from a file.read dump.sql
Quit.quit

Create and change tables

TaskSQL
Create a tableCREATE TABLE products (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE, price REAL NOT NULL CHECK (price >= 0), added TEXT DEFAULT CURRENT_TIMESTAMP);
Create only if missingCREATE TABLE IF NOT EXISTS orders (id INTEGER PRIMARY KEY, product_id INTEGER NOT NULL REFERENCES products(id), qty INTEGER NOT NULL DEFAULT 1);
Create an indexCREATE INDEX IF NOT EXISTS idx_orders_product ON orders(product_id);
Add a columnALTER TABLE products ADD COLUMN stock INTEGER NOT NULL DEFAULT 0;
Rename a columnALTER TABLE products RENAME COLUMN stock TO in_stock;

More: CREATE TABLE, AUTOINCREMENT.

Inspect a database

TaskSQL
List tables with SQLSELECT name FROM sqlite_schema WHERE type = 'table' AND name NOT LIKE 'sqlite\_%' ESCAPE '\';
List a table’s columnsPRAGMA table_info(products);

More: show tables, describe a table.

Insert, update and delete

TaskSQL
Insert rowsINSERT INTO products (name, price) VALUES ('Pen', 1.5), ('Notebook', 4.25), ('Stapler', 9.99);
Insert and return the new rowINSERT INTO orders (product_id, qty) VALUES (1, 3) RETURNING id, product_id, qty;
Insert or update (upsert)INSERT INTO products (name, price) VALUES ('Pen', 1.75) ON CONFLICT(name) DO UPDATE SET price = excluded.price;
Insert, ignoring duplicatesINSERT OR IGNORE INTO products (name, price) VALUES ('Pen', 99);
Update rowsUPDATE products SET in_stock = in_stock + 10 WHERE name = 'Pen';
Delete rowsDELETE FROM products WHERE name = 'Stapler';

More: upsert.

Query

TaskSQL
Filter, sort and limitSELECT name, price FROM products WHERE price < 5 ORDER BY price DESC LIMIT 10;
Pattern match (case-insensitive for ASCII)SELECT name FROM products WHERE name LIKE 'n%';
Group and aggregateSELECT product_id, count(*) AS orders, sum(qty) AS units FROM orders GROUP BY product_id HAVING sum(qty) > 0;
Join two tablesSELECT o.id, p.name, o.qty FROM orders AS o JOIN products AS p ON p.id = o.product_id;
Conditional valueSELECT name, iif(price > 2, 'pricey', 'cheap') AS band FROM products;
Replace NULLSELECT name, coalesce(NULL, 'n/a') AS note FROM products LIMIT 1;
Common table expressionWITH cheap AS (SELECT * FROM products WHERE price < 5) SELECT count(*) FROM cheap;
Window functionSELECT name, price, rank() OVER (ORDER BY price DESC) AS price_rank FROM products;

More: SELECT, window functions.

Strings

TaskSQL
ConcatenateSELECT 'SQL' || 'ite' AS joined;
Substring, upper, lengthSELECT substr('sqldocs', 1, 3) AS part, upper('sql') AS up, length('sqldocs') AS len;
Trim and replaceSELECT trim(' hi ') AS trimmed, replace('a-b-c', '-', '/') AS replaced;

More: string concatenation.

Dates and times

TaskSQL
Current UTC date and timeSELECT datetime('now') IS NOT NULL AS has_now;
Add an intervalSELECT date('2026-10-06', '+7 days') AS next_week;
Start of monthSELECT date('2026-10-06', 'start of month') AS month_start;
Format a dateSELECT strftime('%Y-%m', '2026-10-06') AS year_month;
Days between datesSELECT julianday('2026-12-25') - julianday('2026-10-06') AS days;
Unix seconds to textSELECT datetime(1791297015, 'unixepoch') AS from_unix;

More: SQLite date and time.

JSON

TaskSQL
Read a JSON fieldSELECT '{"a": {"b": 2}}' ->> '$.a.b' AS value;
Set a JSON fieldSELECT json_set('{"a": 1}', '$.b', 2) AS updated;
Expand a JSON arraySELECT value FROM json_each('[10, 20, 30]');

More: SQLite JSON.

Types

TaskSQL
Check a value’s typeSELECT typeof(42), typeof('42'), typeof(4.2), typeof(NULL);
Convert a typeSELECT CAST('42' AS INTEGER) + 1 AS n;

More: SQLite data types.

Transactions

TaskSQL
Start, commitBEGIN IMMEDIATE; UPDATE products SET price = price * 1.1 WHERE id = 2; COMMIT;
Undo uncommitted changesBEGIN; DELETE FROM orders; ROLLBACK;

More: database is locked.

PRAGMAs

TaskSQL
Enforce foreign keys (per connection)PRAGMA foreign_keys = ON;
Switch to WAL mode (persistent)PRAGMA journal_mode = WAL;
Wait up to 5 s for locksPRAGMA busy_timeout = 5000;
Check database integrityPRAGMA integrity_check;

More: WAL mode, foreign keys.

Maintenance

TaskSQL
Analyze query planEXPLAIN QUERY PLAN SELECT * FROM orders WHERE product_id = 1;
Reclaim free spaceVACUUM;
Copy a live database to a fileVACUUM INTO 'backup.db';

More: CREATE INDEX.

Official references: the sqlite3 shell, SQL as understood by SQLite, PRAGMA statements.