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.

Reading & expressions

Select columns and filter records

SQLite

SELECT chooses returned expressions; FROM names the source; WHERE keeps rows whose predicate is true. AS gives an output alias. Prefer explicit columns when the caller expects a stable result shape.

Fragment

SQLite · Select columns and filter records
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT item_id, name AS label, qty
FROM items
WHERE qty > 0
ORDER BY item_id;

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

Distinguish missing, NULL and empty values

SQLite

= NULL does not test absence. Use IS NULL/IS NOT NULL. Comparisons with NULL commonly produce unknown; WHERE keeps true, not false or unknown. COALESCE selects the first non-NULL value; empty text and zero are distinct values.

Fragment

SQLite · Distinguish missing, NULL and empty values
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT item_id, name
FROM items
WHERE category IS NULL;

SELECT item_id, COALESCE(category, 'unassigned') AS category
FROM items
ORDER BY item_id;

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

Combine predicates and membership tests

SQLite

AND/OR/NOT combine predicates under SQL’s NULL rules. Parentheses make grouping explicit. IN tests membership; BETWEEN includes both ends. A NULL inside NOT IN can make the result unknown: use a deliberate null policy or NOT EXISTS. Do not rely on left-to-right short-circuiting to prevent invalid SQL expressions.

Fragment

SQLite · Combine predicates and membership tests
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT item_id
FROM items
WHERE (qty > 0 OR category IS NULL)
  AND name NOT LIKE 'Old%'
  AND item_id IN (1, 3)
ORDER BY item_id;

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

Order, limit and continue a page

SQLite

ORDER BY defines result order; a unique final tie-breaker makes a limited page deterministic. LIMIT/OFFSET selects a slice. Keyset pagination instead continues after every ordering key: qty DESC uses <, then item_id ASC uses > on ties. Bind the previous page’s keys in the caller. Changing data still needs a consistency policy. Use NULLS FIRST/LAST explicitly when nullable sort keys need a portable placement.

Fragment

SQLite · Order, limit and continue a page
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT item_id, name, qty
FROM items
WHERE qty > 0
ORDER BY qty DESC, item_id ASC
LIMIT 1 OFFSET 0;

-- Continue after the previous page's (qty=2, item_id=1).
SELECT item_id, name, qty
FROM items
WHERE qty > 0
  AND (qty < 2 OR (qty = 2 AND item_id > 1))
ORDER BY qty DESC, item_id ASC
LIMIT 1;

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

Calculate and convert values

SQLite

CAST requests a SQLite conversion, which can coerce or truncate rather than validate: CAST('bad' AS INTEGER) yields 0. Integer division truncates when both operands are integers; use a REAL operand for a fractional result. REAL arithmetic is approximate.

Fragment

SQLite · Calculate and convert values
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT 7 / 2, 7 / 2.0;
SELECT CAST('24' AS INTEGER), CAST('bad' AS INTEGER);

Self-contained SQLite expressions.

Choose a conditional or fallback value

SQLite

CASE returns a branch value; ELSE is NULL when omitted. COALESCE chooses the first non-NULL argument. NULLIF(a,b) returns NULL when a=b; it can express a zero-denominator policy. Branch/result types and constant folding still follow the selected engine.

Fragment

SQLite · Choose a conditional or fallback value
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT item_id,
       CASE WHEN qty > 0 THEN 'available'
            ELSE 'empty' END AS state,
       COALESCE(category, 'unassigned') AS category,
       price / NULLIF(qty, 0) AS price_per_unit
FROM items
ORDER BY item_id;

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

Build and inspect text

SQLite

|| concatenates; lower/upper/trim/substr manipulate text. length(TEXT) counts Unicode code points, not UTF-8 bytes; built-in case conversion is ASCII-oriented without extensions. NULL concatenation remains NULL.

Fragment

SQLite · Build and inspect text
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT name || ' (' || item_id || ')' AS label,
       length(name) AS characters,
       substr(name, 1, 3) AS prefix
FROM items ORDER BY item_id;

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

Read dates, timestamps and intervals

SQLite

SQLite date functions accept supported text, Julian-day numbers or Unix timestamps with modifiers. There is no dedicated timestamp storage class; declare the representation, precision and zone policy. datetime returns text.

Fragment

SQLite · Read dates, timestamps and intervals
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT date('2026-10-01', '+1 day');
SELECT datetime(0, 'unixepoch');

Self-contained expressions; datetime epoch output is UTC.