Reference

SQL & Redis

SQLite, PostgreSQL, Snowflake and Redis syntax, queries and everyday operations.

Scope and conventions

SQL examples name their database and assume the sample schema where indicated. Redis examples are native redis-cli commands, with optional Search/JSON features labeled. Run independent examples in your own scratch database; driver binding syntax and transaction guarantees differ.

Indexes & plans

Recognize an index and its access contract

SQLite

A composite index can help predicates/order compatible with its key order; it does not guarantee use or eliminate all sorting. Indexes cost storage and write work. Inspect native index support and maintenance costs for the database/table type.

Fragment

SQLite · Recognize an index and its access contract
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
CREATE INDEX items_category_qty
ON items(category, qty DESC, item_id);

SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.

Explain selected access paths

SQLite

EXPLAIN QUERY PLAN prepares a description of access paths; it does not execute the SELECT. Browser Run captures rows and native statement counters separately. Full-scan steps are not a universal rows-examined total or physical I/O count.

Fragment

SQLite · Explain selected access paths
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
EXPLAIN QUERY PLAN
SELECT item_id FROM items
WHERE category = 'paper' ORDER BY item_id;

SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.

Order the columns of a composite index

SQLite

A composite B-tree index is sorted by its first column, then by the next within each first value, and so on. It can seek on a leftmost prefix: equality on category, or equality on category plus a range on qty. A query that only constrains qty has no prefix to seek on and scans. Equality columns go first and the range or ORDER BY column after them. If every selected column is in the index, the plan says COVERING INDEX and skips the table lookup. In practice: build the index for the WHERE and ORDER BY of a real query, then confirm with EXPLAIN QUERY PLAN. SQLite can sometimes skip-scan a low-cardinality leading column, but only after ANALYZE has collected statistics.

Fragment

SQLite · Order the columns of a composite index
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
CREATE INDEX items_category_qty ON items(category, qty);

EXPLAIN QUERY PLAN
SELECT name FROM items WHERE category = 'paper' AND qty > 0;
-- SEARCH items USING INDEX items_category_qty (category=? AND qty>?)

EXPLAIN QUERY PLAN
SELECT name FROM items WHERE qty > 0;
-- SCAN items   (qty alone is not a leftmost prefix)

SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.

Index a subset of rows or a computed value

SQLite

A partial index (CREATE INDEX ... WHERE) contains only the rows that match its predicate. It is smaller and cheaper to maintain, but it is used only when the query’s WHERE clause implies that predicate. Here qty > 0 matches; qty >= 0 does not. An expression index stores a computed value. The query must use the same expression as the index: lower(name) can use items_name_lower, while name = ... cannot. Expressions may only use deterministic functions and columns of the table. In practice: use a partial index for a hot subset (unshipped orders, active users) and an expression index for case-insensitive or derived lookups, then check both with EXPLAIN QUERY PLAN.

Fragment

SQLite · Index a subset of rows or a computed value
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
CREATE INDEX items_in_stock ON items(category) WHERE qty > 0;
CREATE INDEX items_name_lower ON items(lower(name));

EXPLAIN QUERY PLAN
SELECT item_id FROM items WHERE category = 'paper' AND qty > 0;
-- SEARCH items USING INDEX items_in_stock (category=?)

EXPLAIN QUERY PLAN
SELECT item_id FROM items WHERE lower(name) = 'notebook';
-- SEARCH items USING COVERING INDEX items_name_lower (<expr>=?)

SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.

Read a query plan and decide what to fix

SQLite

EXPLAIN QUERY PLAN prints a tree without running the query. SCAN means reading every row of a table or index. SEARCH means a seek using the listed index constraints. USE TEMP B-TREE FOR ORDER BY means an extra sort step. COVERING INDEX means the table itself is never read. The output has no row counts or timings. For those, time the query in the CLI (.timer on) and run ANALYZE so the planner has statistics. In practice: for a slow query, look for SCAN on a large table or a temp B-tree, add an index matching the WHERE then ORDER BY columns, and confirm the plan changed.

Fragment

SQLite · Read a query plan and decide what to fix
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
EXPLAIN QUERY PLAN
SELECT item_id, qty FROM items WHERE category = 'paper' ORDER BY qty;
-- |--SCAN items
-- `--USE TEMP B-TREE FOR ORDER BY

CREATE INDEX items_category_qty ON items(category, qty);
EXPLAIN QUERY PLAN
SELECT item_id, qty FROM items WHERE category = 'paper' ORDER BY qty;
-- `--SEARCH items USING COVERING INDEX items_category_qty (category=?)

SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.