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

Redis docs Redis JSON ↗

Extract JSON while preserving missing/null/type

Redis

JSON.SET/JSON.GET operate on typed JSON, not hashes. $ paths select array-shaped matches: JSON.GET returns JSON text '[2]', '[null]' and '[]' for numeric, JSON-null and missing paths. JSON.TYPE returns native type arrays ["integer"], ["null"] and []. A missing key is a separate nil reply. Client protocol/library decoding still matters.

Fragment

Redis · Extract JSON while preserving missing/null/type
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
JSON.SET ref:document $ '{"qty":2,"optional":null,"tags":["paper","new"]}'
JSON.GET ref:document $.qty
JSON.GET ref:document $.optional
JSON.GET ref:document $.missing
JSON.TYPE ref:document $.qty
JSON.TYPE ref:document $.optional
JSON.TYPE ref:document $.missing

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Expand a nested array into records

Redis

JSONPath can select array matches, returning a native reply rather than a relational row set. JSON.ARRLEN reads the size and JSON.ARRAPPEND changes the array; these commands do not supply SQL LATERAL/FLATTEN rows.

Fragment

Redis · Expand a nested array into records
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
JSON.SET ref:document $ '{"tags":["paper","new"]}'
JSON.GET ref:document $.tags[*]
JSON.ARRLEN ref:document $.tags

Native commands in one redis-cli session; # lines are explanatory, not commands. Examples use disposable ref:* keys.

Build JSON documents from rows

Redis

Redis has no rows to aggregate, so the application assembles the document. JSON.SET writes a JSON value at a path and JSON.ARRAPPEND appends elements to an array in place. JSON is part of Redis 8 Open Source; earlier versions need the RedisJSON module. In practice: write the denormalized document when the source data changes, and treat it as a derived copy that must be rebuilt if it drifts from the system of record.

Fragment

Redis · Build JSON documents from rows
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
JSON.SET ref:category:paper $ '{"category":"paper","items":[]}'
JSON.ARRAPPEND ref:category:paper $.items '{"id":1,"name":"Notebook","qty":2}' '{"id":2,"name":"Pen","qty":0}'
JSON.GET ref:category:paper $

Native commands in one redis-cli session (Redis 8+, which bundles Search, JSON and time series; earlier versions need the matching modules). # lines are explanatory, not commands. Examples use disposable ref:* keys.

Index and query fields inside JSON

Redis

FT.CREATE ... ON JSON indexes JSONPath fields under an alias. TAG suits exact values such as SKUs, NUMERIC supports ranges, and TEXT is full-text search. Pass query values with PARAMS to avoid escaping punctuation like the hyphen in N-1. The index updates as documents change. In practice: index only the paths you query, since every indexed field adds memory and write work.

Fragment

Redis · Index and query fields inside JSON
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
FT.CREATE ref:orders-sku ON JSON PREFIX 1 ref:order: SCHEMA $.sku AS sku TAG $.qty AS qty NUMERIC
JSON.SET ref:order:1 $ '{"sku":"N-1","qty":2}'
JSON.SET ref:order:2 $ '{"sku":"P-7","qty":1}'
FT.SEARCH ref:orders-sku "@sku:{$sku}" PARAMS 2 sku N-1 RETURN 1 qty DIALECT 2

Native commands in one redis-cli session (Redis 8+, which bundles Search, JSON and time series; earlier versions need the matching modules). # lines are explanatory, not commands. Examples use disposable ref:* keys.