Upstash Documentation

ZRANGE

Get members by index, score, or lex range.
3 min read

Use ZRANGE to read a range of members from a sorted set.

By default <start> and <stop> are zero-based indexes into the ordering, inclusive, and may be negative to count from the end, so 0 -1 returns everything and 0 9 the ten lowest scoring members.

The option that follows changes what those two arguments mean. BYSCORE reads them as score bounds, inclusive unless prefixed with ( and accepting -inf and +inf. BYLEX reads them as member bounds written [member, (member, -, or +, which is only meaningful when all members share the same score. REV reverses the order, and with BYSCORE or BYLEX it also means the bounds are given from the higher one to the lower one. LIMIT <offset> <count> pages through the result and requires BYSCORE or BYLEX. WITHSCORES includes each member's score.

ZRANGE covers everything ZRANGEBYSCORE, ZRANGEBYLEX, ZREVRANGE, and their variants do, and it is the command Redis recommends for new code. Use ZRANGESTORE to store the result instead of returning it.

Syntax#

Arguments#

ArgumentRequiredRepeatableDescription
<key>YesNoRedis key targeted by the command.
<start>YesNoLower bound. Interpreted as a rank by default, as a score with BYSCORE, or lexicographically with BYLEX.
<stop>YesNoUpper bound. Interpreted as a rank by default, as a score with BYSCORE, or lexicographically with BYLEX.
(BYSCORE | BYLEX)NoNoHow the two range arguments are read: BYSCORE as score bounds (inclusive, or exclusive with a ( prefix, and -inf/+inf allowed), BYLEX as member bounds ([member, (member, -, or +), which only makes sense when every member has the same score. They are zero-based indexes when neither is given.
REVNoNoReverse the result order.
LIMIT <offset> <count>NoNoSkip <offset> matches and return at most <count>; a <count> of -1 returns all remaining.
WITHSCORESNoNoInclude sorted-set scores.

Important points#

  • Pair-based results may be flattened into one alternating array in RESP2 while RESP3 preserves nested pairs or a map.
  • RESP2 represents floating-point reply values as bulk strings; RESP3 may use native double replies. Client libraries commonly decode either form to a language number.

Response#

The reply reports the result of the operation. Error replies have the same shape in RESP2 and RESP3 and are surfaced as exceptions by the SDKs below.

ProtocolReply
RESP2Array of members, or flat member/score array with WITHSCORES
RESP3Array of members, or array of member/score pairs with WITHSCORES
Note

Client libraries often decode bulk strings, maps, sets, and numeric strings into language-native values. The table describes the Redis wire reply.

Examples#

TCP examples use the TLS REDIS_URL from the Upstash console. REST examples use UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN.

Redis CLI
@upstash/redis
upstash_redis
ioredis
node-redis
redis-py
go-redis
jedis
redis-rs