0
0
Redisquery~10 mins

ZRANGE and ZREVRANGE for reading in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ZRANGE and ZREVRANGE for reading
Start with Sorted Set
Call ZRANGE or ZREVRANGE
Specify start and stop indexes
Retrieve elements in order
Return list of elements
End
The flow shows how ZRANGE or ZREVRANGE commands read elements from a sorted set by specifying start and stop indexes and returning elements in ascending or descending order.
Execution Sample
Redis
ZADD myset 1 one 2 two 3 three 4 four
ZRANGE myset 0 2
ZREVRANGE myset 0 2
Add elements with scores to a sorted set, then read first three elements in ascending and descending order.
Execution Table
StepCommandStart IndexStop IndexOrderResult
1ZADD myset 1 one 2 two 3 three 4 four---[one, two, three, four] (added)
2ZRANGE myset 0 202Ascending[one, two, three]
3ZREVRANGE myset 0 202Descending[four, three, two]
4ZRANGE myset 3 535Ascending[four]
5ZREVRANGE myset 2 424Descending[two, one]
6ZRANGE myset 5 10510Ascending[] (empty, out of range)
💡 Commands stop after retrieving elements within specified index range or return empty if out of range.
Variable Tracker
VariableAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
myset[one(1), two(2), three(3), four(4)][one, two, three, four][one, two, three, four][one, two, three, four][one, two, three, four][one, two, three, four]
Key Moments - 3 Insights
Why does ZRANGE with start=3 and stop=5 return only one element?
Because the sorted set has only 4 elements (indexes 0 to 3). Index 3 corresponds to 'four', indexes 4 and 5 are out of range, so only 'four' is returned (see execution_table row 4).
Why does ZREVRANGE with start=2 and stop=4 return two elements instead of three?
The reversed order indexes start at 0 for the highest score. Indexes 2 and 3 correspond to 'two' and 'one', but index 4 is out of range, so only two elements are returned (see execution_table row 5).
What happens if the start index is greater than the stop index in ZRANGE?
The command returns an empty list because the range is invalid.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of ZRANGE myset 0 2?
A[one, two]
B[four, three, two]
C[one, two, three]
D[two, three, four]
💡 Hint
Check execution_table row 2 under Result column.
At which step does the command return an empty list?
AStep 6
BStep 3
CStep 4
DStep 2
💡 Hint
Look for '[] (empty, out of range)' in the Result column.
If you want to get elements in descending order starting from the highest score, which command do you use?
AZRANGE
BZREVRANGE
CZADD
DZRANK
💡 Hint
Check the Order column in execution_table rows 2 and 3.
Concept Snapshot
ZRANGE key start stop
- Returns elements from sorted set in ascending order by score
- Indexes start and stop are zero-based
ZREVRANGE key start stop
- Returns elements in descending order by score
- Use to read top scores first
- Out of range indexes return empty list
- Both commands return list of elements only
Full Transcript
This visual execution trace shows how Redis commands ZRANGE and ZREVRANGE read elements from a sorted set. First, elements with scores are added using ZADD. Then ZRANGE retrieves elements in ascending order by score using zero-based start and stop indexes. ZREVRANGE retrieves elements in descending order. The execution table shows commands, indexes, order, and results step-by-step. Variable tracker shows the sorted set contents remain unchanged during reads. Key moments clarify common confusions about index ranges and order. The quiz tests understanding of results and command usage. The snapshot summarizes syntax and behavior for quick reference.