0
0
Redisquery~10 mins

Top-N queries in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Top-N queries
Start with dataset
Sort data by score or value
Select top N items
Return top N results
End
Top-N queries sort data by a score or value and then pick the top N items to return.
Execution Sample
Redis
ZADD leaderboard 100 "Alice" 200 "Bob" 150 "Carol"
ZREVRANGE leaderboard 0 2 WITHSCORES
Add scores for players and get top 3 players with their scores.
Execution Table
StepCommandActionData StateOutput
1ZADD leaderboard 100 "Alice"Add Alice with score 100{"leaderboard": {"Alice": 100}}OK
2ZADD leaderboard 200 "Bob"Add Bob with score 200{"leaderboard": {"Alice": 100, "Bob": 200}}OK
3ZADD leaderboard 150 "Carol"Add Carol with score 150{"leaderboard": {"Alice": 100, "Bob": 200, "Carol": 150}}OK
4ZRANGE leaderboard -3 -1 WITHSCORESGet all 3 members sorted by score ascending{"leaderboard": {"Alice": 100, "Bob": 200, "Carol": 150}}["Alice", "100", "Carol", "150", "Bob", "200"]
5ZREVRANGE leaderboard 0 2 WITHSCORESGet top 3 members sorted by score descending{"leaderboard": {"Alice": 100, "Bob": 200, "Carol": 150}}["Bob", "200", "Carol", "150", "Alice", "100"]
6EndTop-N query complete{"leaderboard": {"Alice": 100, "Bob": 200, "Carol": 150}}Top 3 players with scores returned
💡 All top 3 players retrieved; query ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
leaderboard{}{"Alice": 100}{"Alice": 100, "Bob": 200}{"Alice": 100, "Bob": 200, "Carol": 150}{"Alice": 100, "Bob": 200, "Carol": 150}
Key Moments - 2 Insights
Why does ZRANGE with -3 -1 return all players sorted ascending, not top scores?
ZRANGE returns elements in ascending order by score. To get top scores descending, use ZREVRANGE as shown in step 5.
How does Redis store scores and members in sorted sets?
Redis stores members with their scores in a sorted set, allowing fast retrieval by score order, as seen in the leaderboard variable changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what order are the players returned?
ADescending by score: Bob, Carol, Alice
BRandom order
CAscending by score: Alice, Carol, Bob
DAlphabetical order
💡 Hint
Check the output column at step 4 showing scores from 100 to 200.
At which step does the leaderboard contain all three players?
AAfter Step 1
BAfter Step 3
CAfter Step 2
DAfter Step 4
💡 Hint
Look at variable_tracker for leaderboard state after each step.
If you want the top 2 players by highest score, which command would you use?
AZREVRANGE leaderboard 0 1
BZRANGE leaderboard 0 1
CZRANGE leaderboard -2 -1
DZREVRANGE leaderboard -2 -1
💡 Hint
Refer to step 5 where ZREVRANGE returns highest scores first.
Concept Snapshot
Top-N queries in Redis use sorted sets.
Add members with scores using ZADD.
Use ZREVRANGE to get top N by highest scores.
ZRANGE returns sorted ascending by score.
Top-N queries help find best scoring items quickly.
Full Transcript
This visual execution shows how Redis handles Top-N queries using sorted sets. We add players with scores using ZADD commands. The leaderboard variable tracks the stored data. Using ZRANGE returns members sorted by score ascending, while ZREVRANGE returns them descending, which is useful for Top-N queries. The execution table traces each command, data state, and output. Key moments clarify common confusions about sorting order and data storage. The quiz tests understanding of order, data state, and correct commands for Top-N retrieval.