0
0
Redisquery~10 mins

ZRANGEBYLEX for lexicographic queries in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ZRANGEBYLEX for lexicographic queries
Start with Sorted Set
Define Lex Range: min, max
ZRANGEBYLEX command runs
Filter members by lex order
Return members in lex order
End
ZRANGEBYLEX filters sorted set members by their lexicographic order within a specified range and returns them.
Execution Sample
Redis
ZADD fruits 0 apple 0 banana 0 cherry 0 date 0 fig
ZRANGEBYLEX fruits [banana [date
Add fruits to a sorted set and get members from 'banana' to 'date' lexicographically.
Execution Table
StepActionLex RangeMembers CheckedMembers Returned
1Add members to sorted set--apple, banana, cherry, date, fig
2Run ZRANGEBYLEX with range [banana to [date[banana, date]apple, banana, cherry, date, figbanana, cherry, date
3Return result--banana, cherry, date
💡 All members checked; only those within lex range returned.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Sorted Set 'fruits'emptyapple, banana, cherry, date, figapple, banana, cherry, date, figapple, banana, cherry, date, fig
Lex Rangeundefinedundefined[banana, date][banana, date]
Resultemptyemptybanana, cherry, datebanana, cherry, date
Key Moments - 3 Insights
Why does 'apple' not appear in the result even though it is in the set?
Because 'apple' is lexicographically before the minimum range '[banana' as shown in execution_table row 2, so it is excluded.
What does the '[' symbol mean in the lex range '[banana'?
It means the range includes 'banana' itself (inclusive). This is why 'banana' is included in the result in execution_table row 2.
Why is 'fig' not included in the result?
'fig' is lexicographically after the maximum range '[date', so it is excluded as shown in execution_table row 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which members are returned at step 2?
Abanana, cherry, date
Bapple, banana, cherry
Capple, date, fig
Dcherry, date, fig
💡 Hint
Check the 'Members Returned' column in execution_table row 2.
At which step is the lex range defined?
AStep 1
BStep 3
CStep 2
DLex range is never defined
💡 Hint
Look at the 'Lex Range' column in execution_table and variable_tracker.
If the lex range was changed to (banana [date, which member would be excluded?
Adate
Bbanana
Ccherry
Dapple
💡 Hint
The '(' means exclusive, so check which member matches the lower bound in variable_tracker.
Concept Snapshot
ZRANGEBYLEX key min max
- Returns members in sorted set by lex order
- min and max define lex range
- Use '[' for inclusive, '(' for exclusive
- Members outside range are excluded
- Scores are ignored, only lex order matters
Full Transcript
ZRANGEBYLEX is a Redis command that returns members of a sorted set within a lexicographic range. First, members are added to the sorted set with ZADD. Then, ZRANGEBYLEX is called with a minimum and maximum lex range, using '[' for inclusive and '(' for exclusive bounds. The command checks each member's lex order and returns only those within the range. For example, with members apple, banana, cherry, date, fig, and range [banana to [date, the result is banana, cherry, date. Members outside the range like apple and fig are excluded. This command ignores scores and filters purely by lexicographic order.