w3resource

Redis Sorted Sets: ZREVRANGEBYSCORE

Redis ZREVRANGEBYSCORE Command

Redis ZREVRANGEBYSCORE command is used to return all the elements in the sorted set at the key with a score between maximum and minimum values. In contrary to the default ordering of sorted sets, for this command, the elements are considered to be ordered from high to low scores. The elements having the same score are returned in reverse lexicographical order.

Here are some common uses and scenarios for the "ZREVRANGEBYSCORE" command:

  • Descending Score Queries: Fetch elements within a specified score range in descending order.
  • Leaderboards: Retrieve top players from a leaderboard by their scores in descending order.
  • Ranking Systems: Obtain ranked members within a score range in descending order.
  • Data Analysis: Analyze data by retrieving elements within a specific score range in reverse order.
  • Top-N Results: Get the highest scoring elements efficiently for further processing or display.

Syntax:

ZREVRANGEBYSCORE key max min [WITHSCORES] [LIMIT offset count]

Return Value

Array reply, a list of elements in the specified score range (optionally with their scores).

Example: Redis ZREVRANGEBYSCORE

127.0.0.1:6379> ZADD mysales 1556 Samsung 2000 Nokis 1800 Micromax
(integer) 3
127.0.0.1:6379> ZADD mysales 2200 Sunsui 1800 MicroSoft 2500 LG
(integer) 3
127.0.0.1:6379> ZREVRANGEBYSCORE mysales +inf -inf WITHSCORES
 1) "LG"
 2) "2500"
 3) "Sunsui"
 4) "2200"
 5) "Nokis"
 6) "2000"
 7) "Micromax"
 8) "1800"
 9) "MicroSoft"
10) "1800"
11) "Samsung"
12) "1556"
127.0.0.1:6379> ZREVRANGEBYSCORE mysales 2000 1800 WITHSCORES
1) "Nokis"
2) "2000"
3) "Micromax"
4) "1800"
5) "MicroSoft"
6) "1800"
127.0.0.1:6379> ZREVRANGEBYSCORE mysales +inf 2200 WITHSCORES
1) "LG"
2) "2500"
3) "Sunsui"
4) "2200"

Example: Redis ZREVRANGEBYSCORE: Using '(' to the max, min

127.0.0.1:6379> ZADD mysales 1556 Samsung 2000 Nokis 1800 Micromax
(integer) 3
127.0.0.1:6379> ZADD mysales 2200 Sunsui 1800 MicroSoft 2500 LG
(integer) 3
127.0.0.1:6379> ZREVRANGEBYSCORE mysales +inf -inf WITHSCORES
 1) "LG"
 2) "2500"
 3) "Sunsui"
 4) "2200"
 5) "Nokis"
 6) "2000"
 7) "Micromax"
 8) "1800"
 9) "MicroSoft"
10) "1800"
11) "Samsung"
12) "1556"
127.0.0.1:6379> ZREVRANGEBYSCORE mysales (2100 (1800 WITHSCORES
1) "Nokis"
2) "2000"
127.0.0.1:6379> ZREVRANGEBYSCORE mysales 2100 (1800 WITHSCORES
1) "Nokis"
2) "2000"

Example: Redis ZREVRANGEBYSCORE: Use limit offset count

127.0.0.1:6379> ZADD mysales 1556 Samsung 2000 Nokis 1800 Micromax
(integer) 3
127.0.0.1:6379> ZADD mysales 2200 Sunsui 1800 MicroSoft 2500 LG
(integer) 3
127.0.0.1:6379> ZREVRANGEBYSCORE mysales +inf -inf  WITHSCORES
 1) "LG"
 2) "2500"
 3) "Sunsui"
 4) "2200"
 5) "Nokis"
 6) "2000"
 7) "Micromax"
 8) "1800"
 9) "MicroSoft"
10) "1800"
11) "Samsung"
12) "1556"
127.0.0.1:6379> ZREVRANGEBYSCORE mysales +inf -inf  WITHSCORES LIMIT 0 3
1) "LG"
2) "2500"
3) "Sunsui"
4) "2200"
5) "Nokis"
6) "2000"
127.0.0.1:6379> ZREVRANGEBYSCORE mysales +inf -inf  WITHSCORES LIMIT 3 10
1) "Micromax"
2) "1800"
3) "MicroSoft"
4) "1800"
5) "Samsung"
6) "1556"

Previous: ZREVRANGEBYLEX
Next: ZREVRANK



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/redis/redis-zrevrangebyscore-key-max-min.php