Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What Redis command is used to get the top N highest scores from a sorted set?
easy
A. ZRANGE
B. ZREVRANGE
C. ZADD
D. ZREM

Solution

  1. Step 1: Understand sorted set ordering

    Redis sorted sets store elements with scores. To get highest scores, we want descending order.
  2. Step 2: Identify the correct command

    ZREVRANGE returns elements in descending order by score, perfect for top N queries.
  3. Final Answer:

    ZREVRANGE -> Option B
  4. Quick Check:

    Top N highest = ZREVRANGE [OK]
Hint: Use ZREVRANGE for top scores in descending order [OK]
Common Mistakes:
  • Using ZRANGE which returns lowest scores first
  • Confusing ZADD (adds elements) with query commands
  • Using ZREM which removes elements
2. Which of the following is the correct syntax to get the top 3 highest scoring members from sorted set players?
easy
A. ZREVRANGE players 1 3
B. ZRANGE players 0 2
C. ZREVRANGE players 0 2
D. ZRANGE players -3 -1

Solution

  1. Step 1: Understand index ranges in ZREVRANGE

    Indexes start at 0. To get top 3, use 0 to 2 inclusive.
  2. Step 2: Choose correct command and range

    ZREVRANGE returns highest scores first. So ZREVRANGE players 0 2 gets top 3.
  3. Final Answer:

    ZREVRANGE players 0 2 -> Option C
  4. Quick Check:

    Top 3 highest = ZREVRANGE players 0 2 [OK]
Hint: Use 0 to N-1 indexes with ZREVRANGE for top N [OK]
Common Mistakes:
  • Using ZRANGE which returns lowest scores
  • Starting index at 1 instead of 0
  • Using negative indexes incorrectly
3. Given a sorted set scores with members and scores: {"alice": 50, "bob": 70, "carol": 60}, what is the result of ZREVRANGE scores 0 1 WITHSCORES?
medium
A. ["carol", "60", "alice", "50"]
B. ["alice", "50", "carol", "60"]
C. ["bob", "70", "alice", "50"]
D. ["bob", "70", "carol", "60"]

Solution

  1. Step 1: Order members by descending score

    Scores descending: bob(70), carol(60), alice(50).
  2. Step 2: Select top 2 with scores

    ZREVRANGE 0 1 returns first two: bob and carol with their scores.
  3. Final Answer:

    ["bob", "70", "carol", "60"] -> Option D
  4. Quick Check:

    Top 2 with scores = bob(70), carol(60) [OK]
Hint: ZREVRANGE 0 N-1 gets top N in descending order [OK]
Common Mistakes:
  • Mixing ascending and descending order
  • Forgetting WITHSCORES returns pairs
  • Selecting wrong index range
4. You run ZREVRANGE leaderboard 0 4 but get an empty list. What is the most likely cause?
medium
A. The sorted set leaderboard does not exist or is empty
B. The command syntax is incorrect
C. ZREVRANGE only works with hashes, not sorted sets
D. The indexes 0 to 4 are out of range for the sorted set

Solution

  1. Step 1: Check command syntax validity

    ZREVRANGE syntax is correct for sorted sets, so syntax is not the issue.
  2. Step 2: Understand empty result causes

    If the sorted set does not exist or is empty, ZREVRANGE returns an empty list.
  3. Final Answer:

    The sorted set leaderboard does not exist or is empty -> Option A
  4. Quick Check:

    Empty result means no data in sorted set [OK]
Hint: Empty list means sorted set missing or empty [OK]
Common Mistakes:
  • Assuming syntax error without checking data
  • Thinking ZREVRANGE works on hashes
  • Believing index out of range causes error instead of empty list
5. You want to get the top 5 players with the lowest scores from a sorted set game_scores. Which command should you use?
hard
A. ZRANGE game_scores 0 4
B. ZRANGE game_scores -5 -1
C. ZREVRANGE game_scores -5 -1
D. ZREVRANGE game_scores 0 4

Solution

  1. Step 1: Identify order for lowest scores

    ZRANGE returns elements in ascending order by score, so lowest scores first.
  2. Step 2: Select correct range for top 5

    Indexes 0 to 4 select first 5 elements, which are lowest scores.
  3. Final Answer:

    ZRANGE game_scores 0 4 -> Option A
  4. Quick Check:

    Lowest 5 scores = ZRANGE 0 4 [OK]
Hint: Use ZRANGE 0 N-1 for lowest N scores [OK]
Common Mistakes:
  • Using ZREVRANGE which returns highest scores
  • Using negative indexes incorrectly
  • Confusing ascending and descending order
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