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.
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, qtyFROM itemsWHERE qty > 0ORDER BY item_id;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
= 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, nameFROM itemsWHERE category IS NULL;SELECT item_id, COALESCE(category, 'unassigned') AS categoryFROM itemsORDER BY item_id;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
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_idFROM itemsWHERE (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 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, qtyFROM itemsWHERE qty > 0ORDER BY qty DESC, item_id ASCLIMIT 1 OFFSET 0;-- Continue after the previous page's (qty=2, item_id=1).SELECT item_id, name, qtyFROM itemsWHERE qty > 0 AND (qty < 2 OR (qty = 2 AND item_id > 1))ORDER BY qty DESC, item_id ASCLIMIT 1;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
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);
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_unitFROM itemsORDER BY item_id;
SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently.
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.