0
0
Redisquery~10 mins

ZRANK and ZREVRANK for position in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ZRANK and ZREVRANK for position
Start with Sorted Set
Call ZRANK member
Find member position ascending
Return rank or nil
Call ZREVRANK member
Find member position descending
Return rank or nil
End
ZRANK finds the zero-based rank of a member in ascending order. ZREVRANK finds it in descending order. Both return nil if member not found.
Execution Sample
Redis
ZADD myset 10 "apple" 20 "banana" 15 "cherry"
ZRANK myset "banana"
ZREVRANK myset "banana"
Add members with scores, then get banana's rank ascending and descending.
Execution Table
StepCommandSorted Set StateActionResult
1ZADD myset 10 "apple" 20 "banana" 15 "cherry"{"apple":10, "banana":20, "cherry":15}Add members with scores3 members added
2ZRANK myset "banana"{"apple":10, "banana":20, "cherry":15}Find rank ascending by score2 (apple=0, cherry=1, banana=2)
3ZREVRANK myset "banana"{"apple":10, "banana":20, "cherry":15}Find rank descending by score0 (banana=0, cherry=1, apple=2)
4ZRANK myset "orange"{"apple":10, "banana":20, "cherry":15}Member not foundnil
5ZREVRANK myset "orange"{"apple":10, "banana":20, "cherry":15}Member not foundnil
💡 Commands end after returning rank or nil for requested member.
Variable Tracker
VariableStartAfter ZADDAfter ZRANK bananaAfter ZREVRANK bananaFinal
mysetempty{"apple":10, "banana":20, "cherry":15}{"apple":10, "banana":20, "cherry":15}{"apple":10, "banana":20, "cherry":15}{"apple":10, "banana":20, "cherry":15}
rank_ascnilnil222
rank_descnilnilnil00
Key Moments - 3 Insights
Why does ZRANK return 2 for banana?
Because in ascending order by score, apple(10) is rank 0, cherry(15) is rank 1, banana(20) is rank 2 as shown in execution_table row 2.
Why does ZREVRANK return 0 for banana?
Because in descending order by score, banana(20) is the highest score and gets rank 0, as shown in execution_table row 3.
What happens if the member does not exist?
Both ZRANK and ZREVRANK return nil, meaning no rank found, as shown in execution_table rows 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what rank does ZRANK return for "cherry"?
A0
B1
C2
Dnil
💡 Hint
Check the ascending order ranks in execution_table row 2 where banana is 2 and cherry is before banana.
At which step does ZREVRANK return nil?
AStep 2
BStep 4
CStep 5
DStep 3
💡 Hint
Look at execution_table rows 4 and 5 for nil results; ZREVRANK nil is at step 5.
If "apple" score changed to 25, what would ZRANK "banana" return?
A1
B2
C0
Dnil
💡 Hint
If apple has highest score, banana moves down one rank in ascending order; check variable_tracker for rank changes.
Concept Snapshot
ZRANK key member
- Returns zero-based rank of member by ascending score
- Returns nil if member missing

ZREVRANK key member
- Returns zero-based rank by descending score
- Returns nil if member missing

Ranks start at 0 for lowest or highest score depending on command.
Full Transcript
This visual execution shows how Redis commands ZRANK and ZREVRANK work on a sorted set. First, members apple, banana, and cherry are added with scores 10, 20, and 15. ZRANK returns the position of a member when sorted from lowest to highest score. For banana, this is 2 because apple and cherry have lower scores. ZREVRANK returns the position from highest to lowest score. For banana, this is 0 because it has the highest score. If a member does not exist, both commands return nil. The variable tracker shows the sorted set state and ranks after each command. Key moments clarify why ranks are as they are and what happens if a member is missing. The quiz tests understanding of ranks and nil results. The snapshot summarizes syntax and behavior for quick reference.