0
0
Redisquery~10 mins

Range queries for scoring in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Range queries for scoring
Start with Sorted Set
Specify Score Range
Execute ZRANGEBYSCORE
Redis Filters Members by Score
Return Members in Score Range
End
This flow shows how Redis uses ZRANGEBYSCORE to get members from a sorted set within a score range.
Execution Sample
Redis
ZADD players 10 "Alice" 20 "Bob" 30 "Charlie"
ZRANGEBYSCORE players 15 30
Add players with scores, then get players with scores between 15 and 30.
Execution Table
StepCommandActionScore RangeResult
1ZADD players 10 "Alice" 20 "Bob" 30 "Charlie"Add members with scores-Added 3 members
2ZRANGEBYSCORE players 15 30Query members with scores >=15 and <=3015 to 30["Bob", "Charlie"]
3ZRANGEBYSCORE players (10 20Query members with scores >10 and <=20>10 to 20["Bob"]
4ZRANGEBYSCORE players -inf 25Query members with scores <=25-inf to 25["Alice", "Bob"]
5ZRANGEBYSCORE players 31 +infQuery members with scores >=3131 to +inf[]
6ExitNo more queries-End of range queries
💡 All range queries executed, no more matching members or queries.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
playersempty{"Alice":10, "Bob":20, "Charlie":30}{"Alice":10, "Bob":20, "Charlie":30}{"Alice":10, "Bob":20, "Charlie":30}{"Alice":10, "Bob":20, "Charlie":30}{"Alice":10, "Bob":20, "Charlie":30}{"Alice":10, "Bob":20, "Charlie":30}
query_resultnonenone["Bob", "Charlie"]["Bob"]["Alice", "Bob"][]none
Key Moments - 2 Insights
Why does the query ZRANGEBYSCORE players (10 20 exclude Alice but include Bob?
Because (10 means scores strictly greater than 10, so Alice with score 10 is excluded, but Bob with 20 is included as 20 <= 20.
What happens if the score range has -inf or +inf?
Redis treats -inf as the lowest possible score and +inf as the highest, so it includes all members below or above those bounds respectively, as shown in steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of ZRANGEBYSCORE players 15 30 at step 2?
A["Bob", "Charlie"]
B["Alice", "Bob"]
C["Alice", "Charlie"]
D[]
💡 Hint
Check the 'Result' column in row for step 2 in execution_table.
At which step does the query return an empty list?
AStep 4
BStep 3
CStep 5
DStep 2
💡 Hint
Look for [] in the 'Result' column in execution_table.
If we change the query in step 3 to ZRANGEBYSCORE players 10 20 (without parentheses), what will be included?
AOnly Bob
BAlice and Bob
COnly Alice
DAlice, Bob, and Charlie
💡 Hint
Parentheses mean exclusive, no parentheses mean inclusive; check step 3 query and results.
Concept Snapshot
Redis ZRANGEBYSCORE command:
Syntax: ZRANGEBYSCORE key min max
Returns members with scores between min and max.
Use ( before number for exclusive range.
-min and +max mean unbounded range.
Useful for filtering sorted sets by score.
Full Transcript
This visual execution trace shows how Redis handles range queries for scoring using the ZRANGEBYSCORE command. First, members with scores are added to a sorted set. Then queries specify score ranges to retrieve members within those scores. The trace shows inclusive and exclusive ranges, unbounded ranges using -inf and +inf, and the results returned. Key moments clarify how exclusive ranges exclude boundary scores and how infinite bounds work. The quiz tests understanding of results at different steps and the effect of parentheses on score ranges.