EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Redis Tutorial Redis ZRANGE
Secondary Sidebar
Redis Tutorial
  • Redis
    • Redis Pubsub
    • Redis GUI
    • Redis Database
    • Redis Get All Keys
    • Redis TTL
    • Redis EXPIRE
    • Redis Version
    • Redis EXPIRE
    • Redis List
    • Redis CLI Commands
    • Redis HSET
    • Redis FLUSHDB
    • Redis Key
    • Redis Persistence
    • Redis FLUSHALL
    • Redis pipeline
    • Redis SADD
    • Redis ZADD
    • Redis ZSET
    • Redis Get
    • Redis Replication
    • Redis Delete All Keys
    • Redis Use Cases
    • Redis HMSET
    • Redis Transactions
    • Redis Monitor
    • Redis HGET
    • Redis SETEX
    • Redis Rails
    • RedisJSON
    • Redis Get Key
    • Redis Auth
    • Redis gem
    • Redis Key Count
    • RedisTimeSeries
    • Redis Timeout
    • Redis LPUSH
    • Redis Architecture
    • Redis API
    • Redis Message Queue
    • Redis RPUSH
    • Redis Protocol
    • Redis Data Types
    • Redis Namespace
    • Redis ZRANGE
    • Redis Master Slave
    • Redis Memory Usage
    • Redis WATCH
    • Redis URL
    • Redis Key Value
    • Redis INCR
    • Redis EXISTS
    • Redis INFO
    • Redis HMGET
    • Redis Operator
    • Redis NOAUTH Authentication Required
    • Redis LRANGE
    • Redis RDB
    • Redis TLS
    • Redis HDEL
    • Redis WebSocket
    • Redis HGETALL
    • Redis GETSET
    • Node-RED WebSocket

Redis ZRANGE

Introduction to Redis ZRANGE

Redis ZRANGE performs different types of queries in range by using the index, score, and using lexicographical order. From the version of redis 6.0, the ZRANGE command will replace the ZREVRANGE and other commands. Basically, the redis ZRANGE command is returning the specified range of elements from the sorted set which was stored in the key. The element is considered in an order from a score of lowest to the highest.

Redis ZRANGE

Key Takeaways

  • Redis ZRANGE command is used to fetch the members from the sorted set, which we have defined using the ZRANGE command.
  • We are using the ZRANGE command with the REV argument to reverse the sorted order; this command has many capabilities and will replace ZRANGEbyscore and other multiple commands.

What is Redis ZRANGE?

In redis ZRANGE we are using the lexicographical order of elements by using equal scores. Both the start and stop elements contain zero indexes where we can say that 0 is the first element and 1 is considered as the next element. It will also indicate the negative numbers from the end of the sorted set that we have used.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Redis sorted set contains the non-repeating and unique string types member in an ordered manner. By default, the sorted set is ordered in ascending order by using the score values. The sorted set maintains the zero-based index, from which member is ranked 0, 1, and so on. When the scores of two or more members are similar, lexicographical ordering is used. The sorted set of orders is ordered by using a ranked score.

Redis ZRANGE Sorted Set

We don’t need to order the Redis ZRANGE sorted set element again because it is already in the ordered list. We can quickly query the member. Redis sorted set is built on the data structure that was dual ported and allows us to add, update, remove, and read operations that were used to execute the time complexity operations. In the redis ZRANGE sorted set, the start and stop will contain the zero-based indexes with 0 as the first element and -1 as the last element.

Below example shows how the redis ZRANGE sorted set works as follows:

First, we define the ZADD command to add the key and elements. We are defining the key name as ZRANGE_key and defining the three elements as follows. In the below example we have added three elements but by using ZRANGE sorted set it only shows only 2 elements because we have defined the range as 0 to 1.

Code:

ZADD zrange_key 1 val1 2 val2 3 val3
ZRANGE zrange_key 0 1

Output:

Redis ZRANGE Elements

In the below example, we define the key name with the zadd command as ZRANGE_key2 and define the five elements. While using ZRANGE we are using start as 1 and end as 5, so it will skip the first value and then retrieve the remaining values as follows.

Code:

ZADD zrange_key2 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE zrange_key2 1 5

Output:

Redis ZRANGE Values

Redis ZRANGE Command

The redis ZRANGE command is used to retrieve a range of numbers from a sorted set that was stored in a key using another redis command. The below example shows the ZRANGE command by using byscore argument, we are using starting number as zero and the ending number as 1 and 4 as follows.

Code:

ZADD zrange_key3 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE zrange_key3 0 1 BYSCORE
ZRANGE zrange_key3 0 4 BYSCORE

Output:

Redis ZRANGE - Retrieve a range

In the below example, we are not defining any argument with the ZRANGE command. We are using only the start and end numbers for displaying the elements. We are creating the key name as ZRANGE_key4 and defining the five values with the specified keys as follows.

Code:

ZADD zrange_key4 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE zrange_key4 0 4

Output:

Redis ZRANGE - Key name

In the below example, we are using only the start number as 0 and the end number as -1 for displaying the elements. We are creating the key name as ZRANGE_key5 and defining the five values with the specified keys as follows.

Code:

ZADD zrange_key5 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE zrange_key5 0 -1

Output:

Redis ZRANGE - Five Values

In the below example, we are using the start number as 0 and the end number as 1 for displaying the elements also we are using REV arguments. We are creating the key name as ZRANGE_key6 and defining the three values with the specified key as follows.

Code:

ZADD zrange_key6 1 val1 2 val2 3 val3
ZRANGE zrange_key6 0 1 REV

Output:

Redis ZRANGE - 3 Values

In the below example, we are using the start number as 0 and the end number as 3 for displaying the elements also we are using withscores arguments. We are creating the key name as ZRANGE_key7 and defining the three values with the specified keys as follows.

Code:

ZADD zrange_key7 1 val1 2 val2 3 val3
ZRANGE zrange_key7 0 1 WITHSCORES

Output:

Using withscores arguments.

Redis ZRANGE Arguments

The ZRANGE command contains multiple arguments. Below is the syntax of redis ZRANGE as follows:

Syntax:

ZRANGE name_of_key Start_num end_num [REV] [BYSCORE | BYLEX] [limit offset count]

ZRANGE will contain multiple arguments as follows. Start and end argument is important in the ZRANGE command.

  • Name_of_key – This is defined as a key name that we are using with the ZRANGE command.
  • Start – This is defined as a range query starting value.
  • End – This is defined as a range query of ending value.
  • Byscore – We are specifying the range based on a score.
  • Bylex – We are specifying the range based on the value which was lexicographical.
  • Rev – This argument is reversing the order of returned members, where the index is holding the score which was highest.
  • Limit – This argument is limiting the number which was returned by specified offset.
  • Withscores – As we know ZRANGE command is only returning the members, to return a member with numbers we are using the withscores argument.

Top Redis ZRANGE Elements

In redis ZRANGE command elements is very important. We are using different types of elements in the ZRANGE command. The below example shows that we are using the start and end elements as 0 and 1 respectively.

Code:

ZADD key_zrange 1 val1 2 val2 3 val3
ZRANGE key_zrange0 1

Output:

Start and End Elements

In the below example, we are using 0 and -1 elements to define the redis ZRANGE as follows. We are defining the key name as key_range and defining the five-element value.

Code:

ZADD key_zrange1 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE key_zrange1 0 -1

Output:

using 0 and -1 elements

In the below example, we are using 1 and 1 elements to define the redis ZRANGE as follows. We are defining the key name as key_range2 and defining the five-element value.

Code:

ZADD key_zrange2 1 val1 2 val2 3 val3 4 val4 5 val5
ZRANGE key_zrange2 1 1

Output:

defining the five element value

FAQs

Given below are the FAQs mentioned:

Q1. What is the use of redis ZRANGE command?

Answer: ZRANGE command is used to fetch the range members from the sorted set. We are using different types of ranges in the ZRANGE command.

Q2. Which arguments we are using with ZRANGE command?

Answer: We are using start, end, rev, Bylex, byscore, limit, and withscores arguments with the ZRANGE command.

Q3. What is the use of the withscores argument in the ZRANGE command?

Answer: In redis ZRANGE command withscores argument is used to display the number and values of elements.

Conclusion

In redis ZRANGE we are using lexicographical order of elements by using equal scores. Both the start and stop elements contain zero indexes where we can say that 0 is the first element and 1 is considered as the next element. It performs different types of queries in range by using the index, score, and using lexicographical order.

Recommended Articles

This is a guide to Redis ZRANGE. Here we discuss the introduction, redis ZRANGE sorted set, command, arguments, and elements. You may also have a look at the following articles to learn more –

  1. Redis Get All Keys
  2. Redis GUI
  3. Redis Pubsub
  4. Redis Version
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more