0
0
Redisquery~30 mins

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

Choose your learning style9 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
Need a 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
Need a 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
Need a 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
Need a hint?

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