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.

Records & types

Distinguish text, names and comments

SQLite

Single quotes make string values; double quotes delimit identifiers. Double a single quote inside a string. Some SQLite configurations tolerate legacy double-quoted strings, but do not rely on that ambiguity. -- comments to line end; /* ... */ comments a block. A semicolon terminates a statement.

Fragment

SQLite · Distinguish text, names and comments
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT 'north gate' AS "display label";
SELECT 'it''s ready' AS message;
-- This is a comment, not a value.

Self-contained SQLite statements.

Read native type names

SQLite

SQLite values have NULL, INTEGER, REAL, TEXT or BLOB storage classes. Ordinary columns use type affinity; STRICT tables restrict declared types to INT, INTEGER, REAL, TEXT, BLOB or ANY. Boolean values use integers; there is no separate date/time storage class.

Fragment

SQLite · Read native type names
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT typeof(24), typeof(1.5), typeof('north');
SELECT TRUE, FALSE; -- 1, 0

Self-contained SELECTs. STRICT requires SQLite 3.37+.

Create sample records

SQLite

These small tables supply the names used by query examples. STRICT keeps column types explicit. Foreign-key checking must be enabled on the connection before starting a transaction. Run this setup once in a fresh local scratch database.

Fragment

SQLite · Create sample records
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
PRAGMA foreign_keys = ON;
CREATE TABLE categories (
  category TEXT PRIMARY KEY NOT NULL,
  label TEXT NOT NULL
) STRICT;
CREATE TABLE items (
  item_id INTEGER PRIMARY KEY,
  name TEXT NOT NULL,
  category TEXT REFERENCES categories(category),
  qty INTEGER NOT NULL,
  price REAL NOT NULL
) STRICT;
INSERT INTO categories VALUES ('paper', 'Stationery');
INSERT INTO items VALUES
  (1, 'Notebook', 'paper', 2, 3.50),
  (2, 'Pen', 'paper', 0, 1.25),
  (3, 'Lamp', NULL, 1, 12.00);

Fresh local SQLite database. REAL is approximate; money lessons use an explicit exact representation.

Recognize keys and constraints

SQLite

PRIMARY KEY, UNIQUE, NOT NULL and CHECK express stored rules. A CHECK yielding NULL passes, so use NOT NULL when absence is invalid. Foreign keys require connection-level enforcement. In ordinary rowid tables, non-INTEGER PRIMARY KEY has historical null exceptions; use STRICT or explicit NOT NULL.

Fragment

SQLite · Recognize keys and constraints
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
CREATE TABLE labels (
  label_id INTEGER PRIMARY KEY,
  name TEXT NOT NULL UNIQUE,
  priority INTEGER NOT NULL CHECK (priority >= 0)
) STRICT;

Independent local table definition; foreign-key setup is in “Create sample records”.

Generate an identity

SQLite

Let the database generate a key when the statement omits it; generated keys still need the intended uniqueness contract.

Fragment

SQLite · Generate an identity
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
CREATE TABLE notes (
  note_id INTEGER PRIMARY KEY,
  body TEXT NOT NULL
);
INSERT INTO notes(body) VALUES ('north');

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