How it works · database in a single file
How SQLite works
Your SQL is compiled into a small program, and a tiny virtual machine runs it against one file on disk.
Think of it like this. A librarian who turns your question into a step-by-step plan first, then walks the shelves following it.
SQLite is developed in its own version control system (Fossil). The GitHub repo is the official mirror.
- ✂️ Tokenizer Cuts the SQL text into words and symbols. src/tokenize.c
- 🌿 Parser Checks the grammar and builds a tree of what you asked for. src/parse.y
- 🧭 Planner Chooses how to find the rows: which index, which table first. src/where.c
- ⚙️ Virtual machine Runs the compiled program, one small instruction at a time. src/vdbe.c
- 🌳 B-tree Keeps each table and index sorted, so any row is a few steps away. src/btree.c
- 📄 Pager Reads and writes the file in fixed-size pages, and makes a change all-or-nothing. src/pager.c
Follow one thing through: SELECT name FROM users WHERE id = 7
- The text is cut into tokens: SELECT, name, FROM, users, and so on.src/tokenize.c
- The parser turns the tokens into a tree: which columns, which table, which condition.src/parse.y
- The planner sees that id is the table’s key (when it is), so one direct lookup will do.src/where.c
- A short program is generated: open the table, seek to row 7, read a column, return a row.src/select.c
- The virtual machine runs it. “Seek” walks down the B-tree to the row.src/vdbe.c
- The B-tree asks the pager for the pages it needs, and the pager reads them from the file or its cache.src/pager.c
The clever bit: Every query becomes a program
SQLite doesn’t walk your query directly. It compiles it into bytecode for its own small virtual machine, with instructions like open this table, seek to this row, read column 2. Put EXPLAIN in front of any query and SQLite prints the program. Compiling once and running many times is also why a prepared statement is fast the second time.
Words you’ll meet
- B-tree
- A sorted tree with many keys per node, shaped so that reading one row touches only a few pages.
- page
- A fixed-size block of the file. Everything is read and written a page at a time.
- journal
- A note of what a change is about to overwrite, so a crash halfway can be undone.
- prepared statement
- A query already compiled to bytecode, ready to run again with new values.
Good questions to ask next
- How does an index make a query faster?
- What happens if the power goes out mid-write?
- What is WAL mode?
Open SQLite in Bento and ask them there. It reads the project in your browser, draws its parts as a map you can press, and opens the real code behind each one.
Take SQLite apart yourself.
Free and open source. Nothing to install, no account.
Ideas inside it
More projects, taken apart
Written by hand from the code as it was on 2026-12-04 (commit ea893ee). SQLite belongs to its authors; this page is an independent explanation. Spot a mistake? Tell us.