Bird
Raised Fist0
Redisquery~5 mins

ZRANGEBYSCORE for score-based queries in Redis

Choose your learning style10 modes available
Introduction
ZRANGEBYSCORE helps you find items in a list that have scores within a certain range. It is useful when you want to get data sorted by numbers like points or ratings.
You want to get all players with scores between 50 and 100 in a game leaderboard.
You need to find products priced between $10 and $20 in a store.
You want to list events happening between two dates stored as scores.
You want to filter items by rating scores in a review system.
Syntax
Redis
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
min and max define the score range to filter items.
Use WITHSCORES to get the scores along with the items.
Examples
Get all members with scores from 50 to 100.
Redis
ZRANGEBYSCORE leaderboard 50 100
Get all members with scores up to 75, including their scores.
Redis
ZRANGEBYSCORE leaderboard -inf 75 WITHSCORES
Get the first 3 members with scores between 60 and 90.
Redis
ZRANGEBYSCORE leaderboard 60 90 LIMIT 0 3
Sample Program
Add four players with scores, then get players with scores between 50 and 100 including their scores.
Redis
ZADD leaderboard 40 "Alice" 70 "Bob" 90 "Charlie" 100 "Diana"
ZRANGEBYSCORE leaderboard 50 100 WITHSCORES
OutputSuccess
Important Notes
Use -inf and +inf to specify open-ended ranges (lowest to max or min to highest).
Scores are numbers, so you can use decimals like 75.5.
LIMIT helps to paginate results when you have many items.
Summary
ZRANGEBYSCORE gets items from a sorted set by score range.
You can include scores in the output with WITHSCORES.
Use LIMIT to control how many results you get.

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