Easy Tutorial
❮ Keys Ttl Redis Strings ❯

Redis Zrange Command

Redis Sorted Set (sorted set)

Redis Zrange returns the members within the specified range in a sorted set.

The members are positioned in ascending order of their scores (from smallest to largest).

Members with the same score are arranged in lexicographical order.

If you need the members to be sorted in descending order of their scores (from largest to smallest), use the ZREVRANGE command.

The index parameters start and stop are both zero-based, meaning 0 represents the first member of the sorted set, 1 represents the second member, and so on.

You can also use negative indices, where -1 represents the last member, -2 represents the second-to-last member, and so on.

Syntax

The basic syntax for the redis Zrange command is as follows:

redis 127.0.0.1:6379> ZRANGE key start stop [WITHSCORES]

Available Versions

= 1.2.0

Return Value

A list of sorted set members within the specified range, optionally with their scores.

Examples

redis 127.0.0.1:6379> ZRANGE salary 0 -1 WITHSCORES             # Displays all members of the sorted set
1) "jack"
2) "3500"
3) "tom"
4) "5000"
5) "boss"
6) "10086"

redis 127.0.0.1:6379> ZRANGE salary 1 2 WITHSCORES              # Displays members in the index range 1 to 2
1) "tom"
2) "5000"
3) "boss"
4) "10086"

redis 127.0.0.1:6379> ZRANGE salary 0 200000 WITHSCORES         # Tests the case when the end index exceeds the maximum index
1) "jack"
2) "3500"
3) "tom"
4) "5000"
5) "boss"
6) "10086"

redis > ZRANGE salary 200000 3000000 WITHSCORES                  # Tests the case when the given range does not exist in the sorted set
(empty list or set)

Redis Sorted Set (sorted set)

❮ Keys Ttl Redis Strings ❯