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

Redis

HMGET retrieves fields from a known hash key. Search can filter indexed records and project fields with RETURN; it is a separate query language, not SQL. This query uses the sample Search index and returns positive quantities in stable ID order.

Fragment

Redis · Select columns and filter records
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
HMGET ref:items:1 name qty
FT.SEARCH ref:items "@qty:[(0 +inf]" RETURN 3 item_id name qty SORTBY item_id ASC LIMIT 0 10 DIALECT 2

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

Distinguish missing, NULL and empty values

Redis

A missing key/field, an empty string and the literal text "null" are different. HEXISTS checks field presence; HGET returns a nil reply when absent. A JSON null is a separate typed JSON value, not a missing hash field or SQL NULL.

Fragment

Redis · Distinguish missing, NULL and empty values
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
HSET ref:profile nickname ""
HEXISTS ref:profile nickname
HEXISTS ref:profile missing
HGET ref:profile missing

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

Combine predicates and membership tests

Redis

Search query syntax has its own operators: adjacent clauses intersect, | unions, - negates, TAG braces match tags and [min max] selects numeric ranges. A leading ( excludes a numeric bound. Escaping and field definitions matter.

Fragment

Redis · Combine predicates and membership tests
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
FT.SEARCH ref:items "@category:{paper} @qty:[1 +inf]" RETURN 2 item_id name DIALECT 2

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

Order, limit and continue a page

Redis

Search LIMIT uses offset then count; SORTBY a unique sortable item_id avoids unspecified tie order. ZRANGE instead follows a sorted set’s score/member order and uses inclusive stop indexes. These are different pagination contracts.

Fragment

Redis · Order, limit and continue a page
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
FT.SEARCH ref:items "*" SORTBY item_id ASC LIMIT 0 2 DIALECT 2
ZRANGE ref:ranking 0 1 WITHSCORES

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

Calculate and convert values

Redis

HGET returns a string value; HINCRBY parses an integer field and atomically changes it. HINCRBYFLOAT uses floating-point semantics, not exact decimal money. An invalid numeric field is an error rather than a general-purpose SQL CAST.

Fragment

Redis · Calculate and convert values
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
HINCRBY ref:items:1 qty 1
HGET ref:items:1 qty

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

Choose a conditional or fallback value

Redis

There is no SQL CASE expression in a basic Redis command. Read the exact command reply and branch in the caller, or use a bounded Lua/Redis function when the branch and mutation must happen atomically on the server.

Different native mechanism; no interchangeable statement is implied.

Different native mechanism; no interchangeable operation is implied.

Build and inspect text

Redis

Ordinary Redis strings are byte sequences. APPEND adds bytes; STRLEN counts bytes; GETRANGE uses zero-based, inclusive byte positions. Splitting a multibyte character can produce invalid encoded text.

Fragment

Redis · Build and inspect text
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SET ref:label north
APPEND ref:label " gate"
STRLEN ref:label
GETRANGE ref:label 0 4

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

Read dates, timestamps and intervals

Redis

Redis has no SQL DATE column type. Applications encode timestamps as text or numbers; sorted-set scores can support a declared time ordering. EXPIRE uses seconds and PEXPIRE milliseconds to set a key lifetime, which is different from storing an event time.

Fragment

Redis · Read dates, timestamps and intervals
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
SET ref:session active
PEXPIRE ref:session 60000
PTTL ref:session

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