Bird
Raised Fist0
Redisquery~30 mins

ZRANGEBYSCORE for score-based queries in Redis - Mini Project: Build & Apply

Choose your learning style10 modes available
Using ZRANGEBYSCORE for Score-Based Queries in Redis
📖 Scenario: You are managing a leaderboard for a game. Players have scores stored in a Redis sorted set. You want to find players within certain score ranges.
🎯 Goal: Build Redis commands step-by-step to add players with scores and query players by score ranges using ZRANGEBYSCORE.
📋 What You'll Learn
Create a sorted set called leaderboard with exact player-score pairs
Set a minimum score variable called min_score with a specific value
Use ZRANGEBYSCORE to get players with scores between min_score and a maximum score
Add a final command to get players with scores in a different range including limits
💡 Why This Matters
🌍 Real World
Leaderboards, ranking systems, and any application where you need to find items within score ranges quickly.
💼 Career
Understanding sorted sets and score-based queries is essential for roles involving Redis, caching, and real-time data processing.
Progress0 / 4 steps
1
DATA SETUP: Create the leaderboard sorted set
Add these exact players and scores to a Redis sorted set called leaderboard: "Alice" with score 50, "Bob" with score 70, "Charlie" with score 60, "Diana" with score 80, and "Eve" with score 90. Use the ZADD command for each player.
Redis
Hint

Use ZADD leaderboard score player for each player.

2
CONFIGURATION: Set the minimum score variable
Set a variable called min_score to the value 60. This will be used to filter players with scores greater than or equal to 60.
Redis
Hint

Assign 60 to min_score like min_score=60.

3
CORE LOGIC: Query players with scores between min_score and 80
Use the ZRANGEBYSCORE command to get players from the leaderboard with scores between min_score and 80. Use the exact command format: ZRANGEBYSCORE leaderboard min_score 80.
Redis
Hint

Use ZRANGEBYSCORE leaderboard 60 80 to get players with scores from 60 to 80.

4
COMPLETION: Query players with scores between 70 and 90 with limit
Add a ZRANGEBYSCORE command to get players from leaderboard with scores between 70 and 90, but only return the first 2 players. Use the syntax: ZRANGEBYSCORE leaderboard 70 90 LIMIT 0 2.
Redis
Hint

Use ZRANGEBYSCORE leaderboard 70 90 LIMIT 0 2 to get the first two players in that score range.

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