0
0
Redisquery~10 mins

ZRANGEBYSCORE for score-based queries in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ZRANGEBYSCORE for score-based queries
Start with Sorted Set
Specify min and max scores
ZRANGEBYSCORE command runs
Redis scans sorted set for members with scores in range
Return members sorted by score
End with filtered list
The command takes a sorted set and a score range, then returns all members whose scores fall within that range, sorted by score.
Execution Sample
Redis
ZADD myset 1 "one" 2 "two" 3 "three"
ZRANGEBYSCORE myset 1 2
Add three members with scores, then get members with scores between 1 and 2 inclusive.
Execution Table
StepCommandActionScore RangeMembers FoundOutput
1ZADD myset 1 "one" 2 "two" 3 "three"Add members to sorted set---
2ZRANGEBYSCORE myset 1 2Query members with scores between 1 and 21 to 2["one", "two"]["one", "two"]
3ZRANGEBYSCORE myset 2 3Query members with scores between 2 and 32 to 3["two", "three"]["two", "three"]
4ZRANGEBYSCORE myset (1 3Query members with scores >1 and <=3(1 to 3]["two", "three"]["two", "three"]
5ZRANGEBYSCORE myset 4 5Query members with scores between 4 and 54 to 5[][]
💡 No more members in the score range, query ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
mysetempty{"one":1, "two":2, "three":3}{"one":1, "two":2, "three":3}{"one":1, "two":2, "three":3}{"one":1, "two":2, "three":3}{"one":1, "two":2, "three":3}
Outputnonenone["one", "two"]["two", "three"]["two", "three"][]
Key Moments - 2 Insights
Why does ZRANGEBYSCORE with (1 3 exclude the member with score 1?
Because (1 means exclusive minimum, so scores strictly greater than 1 are included, excluding score 1 itself. See step 4 in execution_table.
What happens if no members fall in the score range?
The command returns an empty list, as shown in step 5 where no members have scores between 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of ZRANGEBYSCORE myset 1 2 at step 2?
A["two", "three"]
B["one", "two"]
C["one"]
D[]
💡 Hint
Check the Output column in row for step 2 in execution_table.
At which step does the query exclude the member with score 1?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the Score Range and Output columns in execution_table for step 4.
If you change the min score in step 5 from 4 to 2, what would the output be?
A["two", "three"]
B["one", "two", "three"]
C[]
D["three"]
💡 Hint
Compare step 3 and step 5 outputs in execution_table.
Concept Snapshot
ZRANGEBYSCORE key min max
- Returns members with scores between min and max
- Inclusive by default, use ( for exclusive
- Results sorted by score ascending
- Returns empty list if no members match
Full Transcript
The ZRANGEBYSCORE command in Redis retrieves members from a sorted set whose scores fall within a specified range. You add members with scores using ZADD. Then, ZRANGEBYSCORE takes a minimum and maximum score and returns all members with scores in that range, sorted by score. You can specify inclusive or exclusive bounds using parentheses. If no members match, it returns an empty list. This visual trace shows adding members and querying different score ranges step-by-step.