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.

Combining & summarizing

Join related records

Redis

Redis does not provide a general relational JOIN. Fetch explicit related keys in the caller, use a deliberately maintained index/materialized structure, or use a bounded server-side operation. Those choices have their own missing-record, consistency and cost contracts.

Different native mechanism; no interchangeable statement is implied.

Different native mechanism; no interchangeable operation is implied.

Ask whether a related record exists

Redis

EXISTS checks key existence, HEXISTS a hash field and SISMEMBER membership in a maintained set. They are direct lookups, not correlated SQL subqueries. A maintained relationship set must be updated consistently with its source records.

Fragment

Redis · Ask whether a related record exists
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
EXISTS ref:items:1
HEXISTS ref:items:1 category
SISMEMBER ref:category:paper 1

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

Group, count and filter summaries

Redis

FT.AGGREGATE groups indexed documents and applies native reducers. LOAD retrieves a source field when needed. GROUPBY 0 forms one aggregate group; GROUPBY 1 @category forms per-category groups. This requires Search and the sample index.

Fragment

Redis · Group, count and filter summaries
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
FT.AGGREGATE ref:items "*" LOAD 2 @qty @category GROUPBY 1 @category REDUCE COUNT 0 AS items REDUCE SUM 1 @qty AS units DIALECT 2

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

Handle duplicates and set operations

Redis

Redis sets contain unique string members. SINTER, SUNION and SDIFF operate on membership, not SQL rows with NULL and column types. Result order is unspecified; use a sorted set when an ordering contract is required.

Fragment

Redis · Handle duplicates and set operations
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SADD ref:a north west
SADD ref:b west east
SINTER ref:a ref:b
SUNION ref:a ref:b
SDIFF ref:a ref:b

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

Name an intermediate query

Redis

Redis has no SQL CTE relation scope or general recursive CTE. A Search aggregation pipeline, bounded Lua function or separately maintained stage is a different mechanism with explicit inputs and lifecycle.

Different native mechanism; no interchangeable statement is implied.

Different native mechanism; no interchangeable operation is implied.

Define a reusable query

Redis

Redis has no SQL CREATE VIEW. A Search index, cached reply or derived key can serve related read needs, but each has its own update, invalidation and freshness contract. Name the source of truth rather than treating a cached structure as automatically current.

Different native mechanism; no interchangeable statement is implied.

Different native mechanism; no interchangeable operation is implied.

Add rank or running context without grouping away rows

Redis

Redis sorted-set rank is a native ranking operation, not a SQL window partition/frame over arbitrary rows. See “Sorted sets and ranking” for ZRANGE/ZRANK; do not translate ROW_NUMBER or a running window into a fake SQL-equivalent command.

Different native mechanism; no interchangeable statement is implied.

Different native mechanism; no interchangeable operation is implied.