0
0
Redisquery~5 mins

Range queries for scoring in Redis

Choose your learning style9 modes available
Introduction

Range queries let you find items with scores between two values. This helps when you want to filter or sort data by numbers like points or prices.

Finding all players with scores between 50 and 100 in a game leaderboard.
Getting products priced between $10 and $50 in an online store.
Listing events happening between two dates stored as timestamps.
Filtering movies with ratings between 7 and 9 stars.
Syntax
Redis
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]

min and max define the score range. Use -inf or +inf for open ranges.

Adding WITHSCORES returns the scores along with the items.

Examples
Get all members with scores from 50 to 100.
Redis
ZRANGEBYSCORE leaderboard 50 100
Get products priced between 10 and 50, showing their prices.
Redis
ZRANGEBYSCORE products 10 50 WITHSCORES
Get first 5 events with scores up to 1609459200 (a timestamp).
Redis
ZRANGEBYSCORE events -inf 1609459200 LIMIT 0 5
Sample Program

This adds four players with scores, then finds those scoring between 50 and 100, showing their scores.

Redis
ZADD leaderboard 30 "Alice" 70 "Bob" 90 "Carol" 110 "Dave"
ZRANGEBYSCORE leaderboard 50 100 WITHSCORES
OutputSuccess
Important Notes

Scores are numbers used to sort members in sorted sets.

Use -inf and +inf to represent negative and positive infinity for open-ended ranges.

LIMIT helps paginate results when you have many matches.

Summary

Range queries filter sorted set members by score.

Use ZRANGEBYSCORE with min and max to specify the range.

WITHSCORES option shows scores alongside members.