Cheatsheet: SQLite

Last updated 2026-09-22

SQLite Specific Commands

List all tables in the database

SELECT name FROM sqlite_master WHERE type='table';

Get schema of a table

PRAGMA table_info(table_name);

List all views in the database

SELECT name FROM sqlite_master WHERE type='view';

Analyze a query

EXPLAIN QUERY PLAN SELECT * FROM table_name;

Display a low-level description of how SQLite will execute the SELECT statement

EXPLAIN SELECT * FROM table_name;

Enable foreign key support

PRAGMA foreign_keys = ON;

Set journal mode to Write-Ahead Logging (WAL)

PRAGMA journal_mode = WAL;

Set cache size

PRAGMA cache_size = 10000;

Set synchronous mode to FULL

PRAGMA synchronous = FULL;

Basic Commands

Create a new table

CREATE TABLE table_name (column1 datatype, column2 datatype, ...);

Insert data into a table

INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...);

Select data from a table

SELECT column1, column2, ... FROM table_name;

Table Modifications

Add a new column to a table

ALTER TABLE table_name ADD column_name datatype;

Delete a table

DROP TABLE table_name;

Update data in a table

UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;

Delete data from a table

DELETE FROM table_name WHERE condition;

Advanced Queries

Select data with a pattern

SELECT * FROM table_name WHERE column_name LIKE 'pattern';

Group data by a column and count occurrences

SELECT column1, COUNT(column2) FROM table_name GROUP BY column1;

Perform an inner join between two tables

SELECT column1, column2 FROM table1 INNER JOIN table2 ON table1.column = table2.column;

Upsert, typing, and CLI dot-commands

Insert a row, or update it on a unique-constraint conflict (upsert)

INSERT INTO users (email, name) VALUES ('ada@example.com', 'Ada')
  ON CONFLICT(email) DO UPDATE SET name = excluded.name;

Insert a row, silently skipping it if the unique constraint conflicts

INSERT INTO users (email, name) VALUES ('ada@example.com', 'Ada')
  ON CONFLICT(email) DO NOTHING;

Check the actual stored type of a value (SQLite uses dynamic typing/type affinity, not strict column types)

SELECT x, typeof(x) FROM t;

List all tables and views (sqlite3 CLI dot-command, not SQL)

.tables

Print the CREATE statement for a specific table (sqlite3 CLI dot-command)

.schema table_name

Back up a live database to a new file (safe copy, run from the shell)

sqlite3 source.db ".backup backup.db"

Export the database as a portable SQL script (run from the shell)

sqlite3 source.db .dump > backup.sql

FAQ