w3resource

Redis Sorted Sets: ZRANGEBYSCORE

Redis ZRANGEBYSCORE Command

Redis ZRANGEBYSCORE command is used to return all the elements in the sorted set at the key with a score between minimum and maximum. The elements are considered to be ordered from low to high scores. The elements having the same score are returned in lexicographical order.

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

  • Leaderboards: Fetch members with scores within a specified range from a leaderboard.
  • Ranking Systems: Retrieve a specified range of ranked members based on their scores.
  • Data Analysis: Retrieve a segment of data for analysis or reporting purposes based on score ranges.
  • Range Queries: Perform score-based queries to fetch elements within a specified score range efficiently.
  • Event Scheduling: Retrieve events scheduled within a specific timeframe based on their scores.

Syntax:

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

Available since

1.0.5.

Return Value

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

Return Value Type

Array

Example: Redis ZRANGEBYSCORE: specify the range to score

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> ZRANGEBYSCORE mysales -inf +inf WITHSCORES
 1) "Samsung"
 2) "1556"
 3) "MicroSoft"
 4) "1800"
 5) "Micromax"
 6) "1800"
 7) "Nokis"
 8) "2000"
 9) "Sunsui"
10) "2200"
11) "LG"
12) "2500"
127.0.0.1:6379> ZRANGEBYSCORE mysales 1500 1800 WITHSCORES
1) "Samsung"
2) "1556"
3) "MicroSoft"
4) "1800"
5) "Micromax"
6) "1800"
127.0.0.1:6379> ZRANGEBYSCORE mysales 2000 +inf WITHSCORES
1) "Nokis"
2) "2000"
3) "Sunsui"
4) "2200"
5) "LG"
6) "2500"

Example: Redis ZRANGEBYSCORE: Using ( to the min, max To avoid include

127.0.0.1:6379> ZRANGEBYSCORE mysales (1500 (1800 WITHSCORES
1) "Samsung"
2) "1556"
127.0.0.1:6379> ZRANGEBYSCORE mysales (1800 2200 WITHSCORES
1) "Nokis"
2) "2000"
3) "Sunsui"
4) "2200"

Example: Redis ZRANGEBYSCORE: Using limit offset count

127.0.0.1:6379> ZRANGEBYSCORE mysales -inf +inf WITHSCORES LIMIT 0 3
1) "Samsung"
2) "1556"
3) "MicroSoft"
4) "1800"
5) "Micromax"
6) "1800"
127.0.0.1:6379> ZRANGEBYSCORE mysales -inf +inf WITHSCORES LIMIT 4 10
1) "Sunsui"
2) "2200"
3) "LG"
4) "2500"

Previous: ZRANGEBYLEX
Next: ZRANK



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-zrangebyscore-key-min-max.php