ZREVRANGEBYSCORE(3valkey)
ZREVRANGEBYSCORE π
okay bestie, this is the Valkey Command Manual page for ZREVRANGEBYSCORE β aka the sorted-set glow-up that hands you your members from biggest score to smallest. no cap. π¦
β¨ NAME β¨
zrevrangebyscore β returns members in a sorted set within a range of scores in reverse order. yes, backwards, on purpose, iconic behavior π
π SYNOPSIS π
ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]
note that it's max first and min second, bestie. that trips everybody up the first time. max min. do not come for me. π
πΈ DESCRIPTION πΈ
So here's the tea β β it returns all the elements in the sorted set at key with a score between max and min, and yes that range is inclusive: elements whose score is exactly equal to max or exactly equal to min are coming to the party too. π
And here's the plot twist: unlike the default ordering of sorted sets, for this command the elements are considered to be ordered from high scores down to low scores. highest first, periodt. π
When multiple elements are out here with the same score, they get returned in reverse lexicographical order. like, alphabetically, but make it backwards. π
Other than the whole reversed-ordering thing, ZREVRANGEBYSCORE is basically ZRANGEBYSCORE's mirror twin. same energy, flipped. πͺ
π§ Alternative
ZRANGE with the REV and BYSCORE arguments. that's it, that's the alternative. iykyk β¨
π REPLY π
valkey-protocol(7)Array reply: a list of the members and, optionally, their scores in the specified score range. you get the scores only if you asked for them withWITHSCORES, obviously bestie. π«Ά
β COMPLEXITY β
O(log(N)+M) β where N is the number of elements in the sorted set and M is the number of elements being returned. math is giving main character energy today. π₯
and here's a lil cheat code: if M is constant (like, say, you're always asking for the first 10 elements with LIMIT), you can consider it O(log(N)). slay. π
π ACL CATEGORIES π
@read@slow@sortedset
three lil badges of honor. π
π¦ HISTORY π¦
- Available since:
2.2.0β a true elder, respectfully. - Changed in
2.1.6:minandmaxcan be exclusive. β yeah, the exclusive thing was added there. note that the "available since" version is numerically higher than the "changed in" version. i don't make the rules, i just report them. π
π EXAMPLES π
watch her work:
127.0.0.1:6379> ZADD myzset 1 "one"
(integer) 1
127.0.0.1:6379> ZADD myzset 2 "two"
(integer) 1
127.0.0.1:6379> ZADD myzset 3 "three"
(integer) 1
127.0.0.1:6379> ZREVRANGEBYSCORE myzset +inf -inf
1) "three"
2) "two"
3) "one"
127.0.0.1:6379> ZREVRANGEBYSCORE myzset 2 1
1) "two"
2) "one"
127.0.0.1:6379> ZREVRANGEBYSCORE myzset 2 (1
1) "two"
127.0.0.1:6379> ZREVRANGEBYSCORE myzset (2 (1
(empty array)
see that? +inf to -inf pulls everything back-to-front β "three", "two", "one". and the parentheses? that's the exclusive flex. (1 means "not 1", so 1 dips out. (2 and (1 together means nothing qualifies, and you get the empty array. a tragedy, honestly. π
π SEE ALSO π
bzmpop(3valkey)bzpopmax(3valkey)bzpopmin(3valkey)zadd(3valkey)zcard(3valkey)zcount(3valkey)zdiff(3valkey)zdiffstore(3valkey)zincrby(3valkey)zinter(3valkey)zintercard(3valkey)zinterstore(3valkey)zlexcount(3valkey)zmpop(3valkey)zmscore(3valkey)zpopmax(3valkey)zpopmin(3valkey)zrandmember(3valkey)zrange(3valkey)zrangebylex(3valkey)zrangebyscore(3valkey)zrangestore(3valkey)zrank(3valkey)zrem(3valkey)zremrangebylex(3valkey)zremrangebyrank(3valkey)zremrangebyscore(3valkey)zrevrange(3valkey)zrevrangebylex(3valkey)zrevrank(3valkey)zscan(3valkey)zscore(3valkey)zunion(3valkey)zunionstore(3valkey)
the whole friend group, fr fr. π«
ok that's the whole page bestie, every flag, every number, every warning, nothing skipped. go forth and query in reverse. π§ββοΈ
this was Valkey 9.0.0, dated 2025-10-21 β and that's the ZREVRANGEBYSCORE(3valkey) page, wrapped with love. mwah. π
π¦ where this page comes from
- fedora 44: valkey-doc 9.0.3-2.fc44 Β· CC-BY-SA-4.0
- fedora 44: valkey-doc 9.0.6-1.fc44 Β· CC-BY-SA-4.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms