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.

Writing & parameters

Insert records with explicit columns

SQLite

An explicit column list makes input mapping visible and lets omitted columns use their defaults. INSERT ... SELECT copies query results under the destination’s constraints. Generated-value/result-returning syntax differs by engine.

Fragment

SQLite · Insert records with explicit columns
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
INSERT INTO items(item_id, name, category, qty, price)
VALUES (4, 'Folder', 'paper', 3, 2.00);

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

Update or delete the intended records

SQLite

UPDATE/DELETE use WHERE to choose rows; no predicate can affect the whole table. RETURNING is available in SQLite 3.35+. An update expression can refer to the old row value; the foreign-key and constraint settings still apply.

Fragment

SQLite · Update or delete the intended records
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
UPDATE items SET qty = qty + 1 WHERE item_id = 1
RETURNING item_id, qty;
DELETE FROM items WHERE item_id = 2;

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

Declare a conflict or merge policy

SQLite

ON CONFLICT targets a uniqueness conflict. DO UPDATE reads proposed values through excluded; it is not a handler for every CHECK or foreign-key error. REPLACE has delete/insert semantics and is not an ordinary in-place update.

Fragment

SQLite · Declare a conflict or merge policy
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
INSERT INTO items(item_id, name, category, qty, price)
VALUES (1, 'Notebook', 'paper', 4, 3.50)
ON CONFLICT(item_id) DO UPDATE SET qty = excluded.qty;

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

Bind values without building query syntax

SQLite

SQLite supports positional and named value parameters. Bind values through your chosen driver or the sqlite3 CLI .parameter interface; placeholder spelling must match the API contract. Parameters do not substitute table/column names, keywords or sort direction. Choose structural options from an allowlist.

Fragment

SQLite · Bind values without building query syntax
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT item_id, name FROM items
WHERE qty >= :minimum
ORDER BY item_id
LIMIT :page_size;

Bind minimum=1 and page_size=2 through the caller; in sqlite3 use .parameter init and .parameter set :minimum 1 / .parameter set :page_size 2 before this SELECT.

Recognize a schema change

SQLite

SQLite ALTER TABLE supports a bounded set of changes; larger restructures can require a documented table-rebuild procedure. New constraints/defaults have restrictions. Do not assume arbitrary ALTER COLUMN syntax from another database works here.

Fragment

SQLite · Recognize a schema change
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
ALTER TABLE items ADD COLUMN note TEXT;
UPDATE items SET note = 'reviewed' WHERE item_id = 1;

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