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.

JSON & nested data

SQLite docs JSON functions ↗

Extract JSON while preserving missing/null/type

SQLite

json_extract returns an SQL scalar for a scalar path; JSON null and a missing path can both become SQL NULL. json_type distinguishes a JSON null (text 'null') from a missing path (SQL NULL). SQLite JSONB and PostgreSQL jsonb are different formats, not portable binary values.

Fragment

SQLite · Extract JSON while preserving missing/null/type
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT json_extract('{"qty":2}', '$.qty');
SELECT json_type('{"qty":null}', '$.qty'),
       json_type('{}', '$.qty');

Self-contained SQLite JSON expressions; requires JSON support.

Expand a nested array into records

SQLite

json_each returns one row per immediate array/object member. Joining it to outer records multiplies rows, so retain the original identity and define empty/missing behavior.

Fragment

SQLite · Expand a nested array into records
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT key, value, type
FROM json_each('["paper","new"]')
ORDER BY key;

Self-contained SQLite table-valued JSON function.

Build JSON documents from rows

SQLite

json_object builds an object from key/value pairs and json_group_array aggregates values into a JSON array. Together they return a nested API-shaped result from one query. Since SQLite 3.44, an aggregate accepts ORDER BY inside its call. Without it, element order is not guaranteed. SQL NULL becomes JSON null. Over zero rows json_group_array returns [], not NULL. REAL prices are serialized as floating-point numbers. In practice: build the response in SQL when it saves a round trip per parent row, and always order the array explicitly.

Fragment

SQLite · Build JSON documents from rows
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SELECT category,
       json_group_array(
         json_object('id', item_id, 'name', name, 'qty', qty)
         ORDER BY item_id
       ) AS items
FROM items
GROUP BY category
ORDER BY category;
-- NULL  | [{"id":3,"name":"Lamp","qty":1}]
-- paper | [{"id":1,"name":"Notebook","qty":2},{"id":2,"name":"Pen","qty":0}]

SQL fragment for the sample items/categories tables in “Create sample records”. Run examples independently. Aggregate ORDER BY requires SQLite 3.44+.

Index and query fields inside JSON

SQLite

SQLite cannot index inside a JSON text value directly. Instead, expose the field as a generated column (or use an expression index on the same extraction) and index that. A VIRTUAL generated column is computed on read and takes no table storage; its index still stores the value. Queries must filter on the generated column or the identical expression. ->> needs SQLite 3.38+ and generated columns 3.31+. In practice: promote the few JSON fields you filter or join on into generated columns, and keep json_valid in a CHECK so bad documents are rejected at write time.

Fragment

SQLite · Index and query fields inside JSON
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
CREATE TABLE orders (
  order_id INTEGER PRIMARY KEY,
  body TEXT NOT NULL CHECK (json_valid(body)),
  sku TEXT GENERATED ALWAYS AS (body ->> '$.sku') VIRTUAL
) STRICT;
CREATE INDEX orders_sku ON orders(sku);
INSERT INTO orders(order_id, body) VALUES
  (1, '{"sku":"N-1","qty":2}'),
  (2, '{"sku":"P-7","qty":1}');

SELECT order_id, body ->> '$.qty' AS qty FROM orders WHERE sku = 'N-1';
EXPLAIN QUERY PLAN SELECT order_id FROM orders WHERE sku = 'N-1';
-- SEARCH orders USING INDEX orders_sku (sku=?)

Self-contained SQLite script (3.38+).