Bird
Raised Fist0
Redisquery~10 mins

ZSCORE for member score in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ZSCORE for member score
Start: Have Sorted Set with members and scores
↓
Call ZSCORE with key and member
↓
Check if member exists in Sorted Set
↓
End
This flow shows how ZSCORE checks if a member exists in a sorted set and returns its score or nil if not found.
Execution Sample
Redis
ZADD leaderboard 100 "Alice"
ZADD leaderboard 150 "Bob"
ZSCORE leaderboard "Alice"
Add two members with scores to a sorted set, then get the score of member "Alice".
Execution Table
StepCommandInputCheck Member Exists?ResultOutput
1ZADDleaderboard 100 "Alice"N/AAdds Alice with score 1001
2ZADDleaderboard 150 "Bob"N/AAdds Bob with score 1501
3ZSCOREleaderboard "Alice"YesAlice found"100"
4ZSCOREleaderboard "Charlie"NoCharlie not foundnil
💡 ZSCORE returns the score if member exists; otherwise returns nil.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
leaderboardempty{"Alice":100}{"Alice":100, "Bob":150}{"Alice":100, "Bob":150}{"Alice":100, "Bob":150}
ZSCORE outputN/AN/AN/A"100"nil
Key Moments - 2 Insights
Why does ZSCORE return nil for a member not in the sorted set?
Because the member does not exist in the sorted set, ZSCORE returns nil to indicate absence, as shown in execution_table row 4.
Is the score returned by ZSCORE a number or a string?
ZSCORE returns the score as a string, not a number. For example, in row 3, the output is "100" (a string).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of ZSCORE for member "Alice" at step 3?
A100
Bnil
C"100"
D"Alice"
💡 Hint
Check the Output column in execution_table row 3.
At which step does the sorted set contain both "Alice" and "Bob"?
AAfter Step 2
BAfter Step 1
CAfter Step 3
DAfter Step 4
💡 Hint
Look at variable_tracker for 'leaderboard' after each step.
If you call ZSCORE for a member not in the set, what will the output be?
A"0"
Bnil
C"" (empty string)
DAn error message
💡 Hint
See execution_table row 4 Output column.
Concept Snapshot
ZSCORE key member
Returns the score of member in sorted set key as a string.
If member not found, returns nil.
Used to get exact score of a member.
Example: ZSCORE leaderboard "Alice" -> "100"
Full Transcript
This visual execution trace shows how the Redis command ZSCORE works. First, members with scores are added to a sorted set using ZADD. Then, ZSCORE is called with the sorted set key and a member name. If the member exists, ZSCORE returns the score as a string. If the member does not exist, it returns nil. The execution table shows each step, the commands run, checks for member existence, and the output. The variable tracker shows how the sorted set changes after each command. Key moments clarify common confusions such as why nil is returned for missing members and the data type of the returned score. The quiz tests understanding of outputs and state changes. The snapshot summarizes the command usage and behavior.

Practice

(1/5)
1. What does the Redis command ZSCORE do?
easy
A. Returns all members of a sorted set
B. Deletes a member from a sorted set
C. Returns the score of a member in a sorted set
D. Adds a member with a score to a sorted set

Solution

  1. Step 1: Understand the purpose of ZSCORE

    The ZSCORE command is used to get the score associated with a specific member in a sorted set.
  2. Step 2: Compare with other commands

    Other commands like ZADD add members, ZREM deletes members, and ZRANGE lists members, so they do not return scores directly.
  3. Final Answer:

    Returns the score of a member in a sorted set -> Option C
  4. Quick Check:

    ZSCORE = get member score [OK]
Hint: ZSCORE always fetches a member's score in sorted sets [OK]
Common Mistakes:
  • Confusing ZSCORE with ZADD or ZREM
  • Thinking ZSCORE returns members instead of scores
  • Assuming ZSCORE modifies the sorted set
2. Which of the following is the correct syntax to get the score of member user42 from sorted set leaderboard in Redis?
easy
A. ZSCORE leaderboard user42
B. ZSCORE user42 leaderboard
C. ZGETSCORE leaderboard user42
D. ZGET leaderboard user42

Solution

  1. Step 1: Recall the correct command format

    The correct syntax for ZSCORE is: ZSCORE key member. Here, leaderboard is the key and user42 is the member.
  2. Step 2: Eliminate incorrect options

    Options B, C, and D either reverse the order or use invalid commands like ZGETSCORE or ZGET, which do not exist.
  3. Final Answer:

    ZSCORE leaderboard user42 -> Option A
  4. Quick Check:

    ZSCORE key member = ZSCORE leaderboard user42 [OK]
Hint: Remember: ZSCORE key member order is fixed [OK]
Common Mistakes:
  • Swapping key and member positions
  • Using non-existent commands like ZGET or ZGETSCORE
  • Missing the key or member argument
3. Given the sorted set players with members and scores: {"alice": 50, "bob": 75, "carol": 60}, what will be the result of ZSCORE players bob?
medium
A. 75
B. 50
C. 60
D. nil

Solution

  1. Step 1: Identify the member's score in the sorted set

    Member bob has a score of 75 in the players sorted set.
  2. Step 2: Understand ZSCORE output

    The command ZSCORE players bob returns the score of bob, which is 75.
  3. Final Answer:

    75 -> Option A
  4. Quick Check:

    bob's score = 75 [OK]
Hint: ZSCORE returns the exact score of the member [OK]
Common Mistakes:
  • Confusing scores of different members
  • Expecting a list instead of a single score
  • Assuming nil if member exists
4. You run the command ZSCORE leaderboard player1 but get (nil) as a result. What could be the reason?
medium
A. The member player1 has a score of 0
B. The member player1 is not in the sorted set
C. The command syntax is incorrect
D. Redis server is down

Solution

  1. Step 1: Understand what (nil) means in ZSCORE

    When ZSCORE returns (nil), it means the member does not exist in the sorted set.
  2. Step 2: Check other possibilities

    A score of 0 for player1 would return "0", not (nil). Syntax errors cause error messages, not (nil). Server down would cause connection errors.
  3. Final Answer:

    The member player1 is not in the sorted set -> Option B
  4. Quick Check:

    Member missing = (nil) result [OK]
Hint: (nil) means member not found in sorted set [OK]
Common Mistakes:
  • Thinking (nil) means the score is 0
  • Assuming (nil) means syntax error
  • Thinking (nil) means server issue
5. You have a sorted set tasks with members and scores: {"task1": 10, "task2": 20, "task3": 30}. You want to check if task4 exists and get its score or return 0 if it doesn't. Which Redis command sequence achieves this?
hard
A. Use ZRANK tasks task4 to get score or 0
B. Use ZGET tasks task4 to get score or 0
C. Use ZCOUNT tasks task4 task4 to get score or 0
D. Use ZSCORE tasks task4 and if result is nil, treat score as 0

Solution

  1. Step 1: Identify how to get a member's score safely

    ZSCORE returns the score or (nil) if the member does not exist.
  2. Step 2: Handle missing member case

    Since ZSCORE returns (nil) for missing members, you can check this result and treat it as 0 in your application logic.
  3. Step 3: Evaluate other options

    ZGET does not exist, ZRANK returns rank, not score, and ZCOUNT counts members in score range, not score value.
  4. Final Answer:

    Use ZSCORE tasks task4 and if result is nil, treat score as 0 -> Option D
  5. Quick Check:

    ZSCORE + nil check = safe score retrieval [OK]
Hint: Use ZSCORE and handle nil as zero in your code [OK]
Common Mistakes:
  • Using non-existent commands like ZGET
  • Confusing rank with score using ZRANK
  • Using ZCOUNT which counts members, not scores
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard