Bird
Raised Fist0
Redisquery~10 mins

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

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

Practice

(1/5)
1. What does the Redis command ZRANGEBYLEX do?
easy
A. Returns members sorted by their score in descending order.
B. Returns members in a sorted set within a lexicographic range.
C. Deletes members from a sorted set based on lexicographic order.
D. Adds members to a sorted set with a lexicographic score.

Solution

  1. Step 1: Understand the command purpose

    ZRANGEBYLEX is used to get members from a sorted set based on their alphabetical order, not their score.
  2. Step 2: Compare options with command behavior

    Only Returns members in a sorted set within a lexicographic range. correctly describes that it returns members within a lexicographic range. Other options confuse sorting by score or modifying the set.
  3. Final Answer:

    Returns members in a sorted set within a lexicographic range. -> Option B
  4. Quick Check:

    ZRANGEBYLEX = Lexicographic range retrieval [OK]
Hint: ZRANGEBYLEX fetches by alphabetical order, not score [OK]
Common Mistakes:
  • Confusing lexicographic order with score order
  • Thinking it deletes or adds members
  • Assuming it sorts in descending order
2. Which of the following is the correct syntax to get members from 'myzset' between 'a' and 'c' inclusive using ZRANGEBYLEX?
easy
A. ZRANGEBYLEX myzset [a [c
B. ZRANGEBYLEX myzset (a (c
C. ZRANGEBYLEX myzset a c
D. ZRANGEBYLEX myzset {a {c

Solution

  1. Step 1: Recall syntax for inclusive range

    In ZRANGEBYLEX, square brackets [ ] mean inclusive boundaries.
  2. Step 2: Match syntax with options

    ZRANGEBYLEX myzset [a [c uses [a and [c, correctly including both 'a' and 'c'. Other options use parentheses or braces which mean exclusive or invalid.
  3. Final Answer:

    ZRANGEBYLEX myzset [a [c -> Option A
  4. Quick Check:

    Inclusive range uses [ ] brackets [OK]
Hint: Use [ ] for inclusive, ( ) for exclusive in ZRANGEBYLEX [OK]
Common Mistakes:
  • Using parentheses instead of brackets for inclusive range
  • Omitting brackets causing syntax errors
  • Using curly braces which are invalid here
3. Given a sorted set 'myzset' with members: ['apple', 'banana', 'carrot', 'date', 'egg'], what is the result of ZRANGEBYLEX myzset [banana (date?
medium
A. ['banana', 'carrot', 'date', 'egg']
B. ['banana', 'carrot', 'date']
C. ['carrot', 'date']
D. ['banana', 'carrot']

Solution

  1. Step 1: Understand the range boundaries

    Start is inclusive [banana], end is exclusive (date), so 'date' is not included.
  2. Step 2: Select members in range

    Members between 'banana' and before 'date' are 'banana' and 'carrot'. 'date' and 'egg' are excluded.
  3. Final Answer:

    ['banana', 'carrot'] -> Option D
  4. Quick Check:

    Inclusive start, exclusive end excludes 'date' [OK]
Hint: Remember ( ) excludes the boundary member [OK]
Common Mistakes:
  • Including 'date' despite exclusive end
  • Including 'egg' which is outside range
  • Confusing inclusive and exclusive boundaries
4. You run ZRANGEBYLEX myzset [a [z LIMIT 0 2 but get an error. What is the likely cause?
medium
A. LIMIT requires both offset and count as integers.
B. LIMIT must come before the range arguments.
C. Range boundaries must use parentheses, not brackets.
D. myzset does not exist, causing the error.

Solution

  1. Step 1: Check LIMIT syntax

    LIMIT requires two integer arguments: offset and count. If either is missing or not integer, error occurs.
  2. Step 2: Compare options

    LIMIT requires offset and count to be valid integers; non-integer values (e.g., strings like "two" or malformed) cause syntax errors. Other options are incorrect: LIMIT follows the range arguments (not B), brackets are valid for inclusive (not C), non-existent key returns empty list (not D).
  3. Final Answer:

    LIMIT requires both offset and count as integers. -> Option A
  4. Quick Check:

    LIMIT needs two integer args [OK]
Hint: LIMIT needs two integers: offset and count [OK]
Common Mistakes:
  • Using LIMIT with missing or non-integer arguments
  • Placing LIMIT before range arguments
  • Confusing bracket types for range boundaries
5. You want to get exactly 3 members from 'myzset' starting after 'apple' (exclusive) up to 'date' (inclusive). Which command is correct?
hard
A. ZRANGEBYLEX myzset (apple (date LIMIT 0 3
B. ZRANGEBYLEX myzset [apple [date LIMIT 1 3
C. ZRANGEBYLEX myzset (apple [date LIMIT 0 3
D. ZRANGEBYLEX myzset [apple (date LIMIT 0 3

Solution

  1. Step 1: Set correct range boundaries

    Start after 'apple' means exclusive start: (apple). End up to 'date' inclusive means [date].
  2. Step 2: Use LIMIT to get 3 members from start

    LIMIT 0 3 means start at offset 0 and get 3 members.
  3. Step 3: Verify options

    Only ZRANGEBYLEX myzset (apple [date LIMIT 0 3 matches exclusive start, inclusive end, and correct LIMIT usage.
  4. Final Answer:

    ZRANGEBYLEX myzset (apple [date LIMIT 0 3 -> Option C
  5. Quick Check:

    Exclusive start ( ), inclusive end [ ], LIMIT 0 3 [OK]
Hint: Use ( for exclusive start, [ for inclusive end, LIMIT 0 3 for 3 items [OK]
Common Mistakes:
  • Using inclusive start when exclusive is needed
  • Mixing up LIMIT offset and count
  • Using wrong bracket types for boundaries
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