Bird
Raised Fist0
Redisquery~10 mins

ZRANGEBYSCORE for score-based queries in Redis - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get all members with scores between 10 and 20.

Redis
ZRANGEBYSCORE mysortedset [1] 20
Drag options to blanks, or click blank then click option'
A10
B5
C30
D-inf
Attempts:
3 left
💡 Hint
Common Mistakes
Using a score lower than 10 will include unwanted members.
Using a string instead of a number for the score.
2fill in blank
medium

Complete the code to get members with scores greater than or equal to 15.

Redis
ZRANGEBYSCORE mysortedset [1] +inf
Drag options to blanks, or click blank then click option'
A15
B(15
C-inf
D20
Attempts:
3 left
💡 Hint
Common Mistakes
Using (15 excludes score 15 itself.
Using -inf will include all scores.
3fill in blank
hard

Fix the error in the code to exclude members with score 10 but include scores above 10.

Redis
ZRANGEBYSCORE mysortedset [1] 20
Drag options to blanks, or click blank then click option'
A[10
B10
C(10
D-inf
Attempts:
3 left
💡 Hint
Common Mistakes
Using 10 includes score 10.
Using [10 is invalid syntax.
4fill in blank
hard

Fill both blanks to get members with scores strictly between 5 and 15.

Redis
ZRANGEBYSCORE mysortedset [1] [2]
Drag options to blanks, or click blank then click option'
A(5
B5
C(15
D15
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 or 15 includes those scores.
Using brackets [ ] is invalid in Redis score ranges.
5fill in blank
hard

Fill all three blanks to get members with scores from 0 (inclusive) to 100 (exclusive), limiting to 10 results starting from the second member (offset 1).

Redis
ZRANGEBYSCORE mysortedset [1] [2] LIMIT [3] 10
Drag options to blanks, or click blank then click option'
A0
B100
C(100
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 100 includes score 100.
Using LIMIT 0 10 starts from first member, not second.
Using 0 as offset starts from the first member.

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