Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Redis command ZRANGEBYSCORE do?
easy
A. It returns the rank of a member in a sorted set.
B. It deletes members from a sorted set by score.
C. It adds a member with a score to a sorted set.
D. It returns members in a sorted set within a given score range.

Solution

  1. Step 1: Understand the command purpose

    ZRANGEBYSCORE is used to get members from a sorted set based on their scores.
  2. Step 2: Compare options with command behavior

    Only It returns members in a sorted set within a given score range. correctly describes returning members within a score range.
  3. Final Answer:

    It returns members in a sorted set within a given score range. -> Option D
  4. Quick Check:

    ZRANGEBYSCORE = returns members by score range [OK]
Hint: ZRANGEBYSCORE fetches by score range, not by rank or deletion [OK]
Common Mistakes:
  • Confusing ZRANGEBYSCORE with ZREM (deletion)
  • Thinking it returns rank instead of members
  • Mixing it up with ZADD (adding members)
2. Which of the following is the correct syntax to get members with scores between 10 and 20 from sorted set myzset including their scores?
easy
A. ZRANGEBYSCORE myzset 10 20 INCLSCORES
B. ZRANGEBYSCORE myzset 10 20 WITHSCORE
C. ZRANGEBYSCORE myzset 10 20 WITHSCORES
D. ZRANGEBYSCORE myzset 10 20 SCORES

Solution

  1. Step 1: Recall correct option for including scores

    The correct keyword to include scores in output is WITHSCORES.
  2. Step 2: Check syntax correctness

    ZRANGEBYSCORE myzset 10 20 WITHSCORES uses the exact correct syntax: ZRANGEBYSCORE myzset 10 20 WITHSCORES.
  3. Final Answer:

    ZRANGEBYSCORE myzset 10 20 WITHSCORES -> Option C
  4. Quick Check:

    WITHSCORES keyword includes scores [OK]
Hint: Use WITHSCORES exactly to get scores with members [OK]
Common Mistakes:
  • Misspelling WITHSCORES
  • Using WITHSCORE or SCORES instead
  • Omitting WITHSCORES when scores are needed
3. Given the sorted set myzset with members and scores:
"a"=5, "b"=10, "c"=15, "d"=20, "e"=25
What is the result of ZRANGEBYSCORE myzset 10 20 LIMIT 1 2?
medium
A. ["c", "d"]
B. ["b", "c", "d"]
C. ["b", "c"]
D. ["a", "b"]

Solution

  1. Step 1: Identify members in score range 10 to 20

    Members with scores 10, 15, 20 are "b", "c", "d".
  2. Step 2: Apply LIMIT 1 2

    LIMIT 1 2 means skip 1 member, then return next 2 members. Skipping "b", next two are "c", "d".
  3. Final Answer:

    ["c", "d"] -> Option A
  4. Quick Check:

    LIMIT skips then returns count [OK]
Hint: LIMIT offset count skips offset, returns count members [OK]
Common Mistakes:
  • Ignoring LIMIT offset and returning from start
  • Confusing score range with member count
  • Including members outside score range
4. You run the command ZRANGEBYSCORE myzset (10 20 but get an error. What is the likely cause?
medium
A. Syntax error due to missing closing parenthesis after 20.
B. Incorrect use of parentheses for exclusive range; missing closing parenthesis.
C. Command is missing the key name.
D. Missing closing parenthesis for exclusive range syntax.

Solution

  1. Step 1: Analyze the command syntax

    The command uses (10 to indicate exclusive minimum score 10, which is correct syntax.
  2. Step 2: Identify the error

    The command ends with 20 without closing parenthesis or space, causing syntax error.
  3. Final Answer:

    Syntax error due to missing closing parenthesis after 20. -> Option A
  4. Quick Check:

    Exclusive range needs parentheses on score values [OK]
Hint: Check parentheses carefully for exclusive score ranges [OK]
Common Mistakes:
  • Forgetting parentheses for exclusive ranges
  • Missing spaces between arguments
  • Using parentheses incorrectly around scores
5. You want to get the top 3 members with scores between 0 and 100 from sorted set myzset, including their scores, but only if their score is greater than 50. Which command achieves this?
hard
A. ZRANGEBYSCORE myzset 50 100 WITHSCORES LIMIT 0 3
B. ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3
C. ZRANGEBYSCORE myzset [50 100 WITHSCORES LIMIT 0 3
D. ZRANGEBYSCORE myzset (50 100 LIMIT 3 WITHSCORES

Solution

  1. Step 1: Define score range with exclusive lower bound

    To get scores greater than 50, use (50 for exclusive minimum.
  2. Step 2: Include scores and limit results

    Use WITHSCORES to include scores and LIMIT 0 3 to get top 3 members.
  3. Step 3: Verify command syntax

    ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3 correctly uses ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3.
  4. Final Answer:

    ZRANGEBYSCORE myzset (50 100 WITHSCORES LIMIT 0 3 -> Option B
  5. Quick Check:

    Exclusive range + WITHSCORES + LIMIT = correct command [OK]
Hint: Use (score for exclusive, WITHSCORES, then LIMIT offset count [OK]
Common Mistakes:
  • Using inclusive 50 instead of exclusive
  • Placing LIMIT before WITHSCORES
  • Using wrong bracket types for exclusive range
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