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

This cheat sheet puts the everyday sqlite3 shell commands and SQL statements on one page, grouped by task, for when you need the exact syntax without a full guide. I tested every line, and in this article I’ll walk you through the groups one by one, with a link to the full guide for each.

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

How to use this page

Find the task in the left column of a table and copy the command next to it. Every line on this page ran top to bottom in a single sqlite3 session against one shop.db database, with no errors, so the statements also work in that order as a quick tour. I also checked the files it writes separately, from the exported CSV to the VACUUM INTO backup.

sqlite3 shell

These dot-commands only work in the sqlite3 shell. They control the session, not the data.

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

These statements define tables and indexes. IF NOT EXISTS makes them safe to run again.

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

Use these to see what a database contains, from code or from the shell.

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

These statements change rows. RETURNING and ON CONFLICT save a second query.

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

These are the SELECT patterns for filtering, grouping and joining.

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

SQLite joins strings with ||, and its string functions count characters, not bytes.

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

SQLite has no date type. These functions work on ISO-8601 text and Unix seconds.

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

JSON is stored as text, and these functions and operators read and change it.

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

SQLite stores five kinds of values. typeof() shows which one a value has.

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

Group changes so they all apply or none do.

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

PRAGMAs read and change SQLite settings. Some last only for the current connection.

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

Use these to check query plans and keep the file compact.

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.

Common mistakes

Watch for these when you copy from this page:

  • Dot-commands are shell-only. .tables and .schema work in the sqlite3 shell, not from Python or Node.js. Use the SQL forms in “Inspect a database” from code.
  • Per-connection PRAGMAs. foreign_keys and busy_timeout reset on every new connection, while journal_mode = WAL is stored in the file. See SQLite PRAGMA.
  • INSERT OR REPLACE isn’t an upsert. It deletes the old row. Use ON CONFLICT … DO UPDATE as shown above.

Further reading

Tested with SQLite 3.51.0 on 7 October 2026.