0
0
Redisquery~5 mins

ZRANGEBYSCORE for score-based queries in Redis

Choose your learning style9 modes available
Introduction
ZRANGEBYSCORE helps you find items in a list that have scores within a certain range. It is useful when you want to get data sorted by numbers like points or ratings.
You want to get all players with scores between 50 and 100 in a game leaderboard.
You need to find products priced between $10 and $20 in a store.
You want to list events happening between two dates stored as scores.
You want to filter items by rating scores in a review system.
Syntax
Redis
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
min and max define the score range to filter items.
Use WITHSCORES to get the scores along with the items.
Examples
Get all members with scores from 50 to 100.
Redis
ZRANGEBYSCORE leaderboard 50 100
Get all members with scores up to 75, including their scores.
Redis
ZRANGEBYSCORE leaderboard -inf 75 WITHSCORES
Get the first 3 members with scores between 60 and 90.
Redis
ZRANGEBYSCORE leaderboard 60 90 LIMIT 0 3
Sample Program
Add four players with scores, then get players with scores between 50 and 100 including their scores.
Redis
ZADD leaderboard 40 "Alice" 70 "Bob" 90 "Charlie" 100 "Diana"
ZRANGEBYSCORE leaderboard 50 100 WITHSCORES
OutputSuccess
Important Notes
Use -inf and +inf to specify open-ended ranges (lowest to max or min to highest).
Scores are numbers, so you can use decimals like 75.5.
LIMIT helps to paginate results when you have many items.
Summary
ZRANGEBYSCORE gets items from a sorted set by score range.
You can include scores in the output with WITHSCORES.
Use LIMIT to control how many results you get.