Syntax and Commands of Redis Sets
1. “sadd” command:
Creates a set, and adds element to it
Syntax: sadd set_name set_element
Example: sadd SocialMedia Facebook Twitter WhatsApp
Explanation: We created a set namely “SocialMedia” and added 3 unique elements into it as “Facebook”, “Twitter”, “WhatsApp”.
Output:
2. “smembers” command:
Shows all the elements, present in that set
Syntax: smembers set_name
Example: smembers SocialMedia
Explanation: Previously, we stored “Facebook”, “Twitter”, “WhatsApp” in SocialMedia set. Hence, it’s displaying these elements.
Output:
3. “scard” command:
Shows no. of elements, present in that set
Syntax: scard set_name
Example: scard SocialMedia
Explanation: There ae 3 elements in this set, hence its showing 3 as output
Output:
4. “sismember” command:
Checks if that element exists in that set, if yes then returns 1
Syntax: sismember set_name set_element
Example: sismember SocialMedia Twitter
Explanation: Returned 1, “Twitter” is present in SocialMedia set.
Output:
5. “sdiff” command:
Shows difference of two sets by displaying those elements, elements that are in set 1 butt not in set 2
Syntax: sdiff set_name1 set_name2
Example: sdiff FavSub1 FavSub2
Explanation: FavSub1 and FavSub2 are two sets that contain favorite subjects of User1 and User2, respectively. Elements of FavSub1 and FavSub2 are {Computer Science, Data Science} and {Computer Science, Math’s}. The ouput of this command is showing diffrence of two sets as their elements that is the elements that are in FavSub1 but not in FavSub2. Hence, output is Data Science.
Output:
6. “sdiffstore” command:
Stores elements that differ in two sets, elements that are in set1 but not in set2 in a new set
Syntax: sdiffstore set_name3 set_name1 set_name2
Example: sdiffstore NewSet FavSub1 FavSub2
Explanation: As the elements that differ in FavSub1 and FavSub2 is only Data Science. Hence, we made a new set namely “NewSet” that stored this element.
Output:
7. “sinter” command:
Shows elements that are present in both sets that is intersection of 2 given sets
Syntax: sinter set_name1 set_name2
Example: sinter FavSub1 FavSub2
Explanation: Displaying Computer Science, as its the only common element present in both sets
Output:
8. “sinterstore” command:
Stores elements that are present in both sets in a new set
Syntax: sinterstore set_name4 set_name1 set_name2
Example: sinterstore SamePinch FavSub1 FavSub2
Explanation: SamePinch is a new set created that stores the elements that are common in FavSub1 and FavSub2 that is Computer Science
Output:
9. “sunion” command:
Shows all the unique elements that are there overall including those 2 sets as union of sets
Syntax: sunion set_name1 set_name2
Example: sunion FavSub1 FavSub2
Explanation: Displaying Computer Science, Maths and Data Science as these are the unique elements in whole as union of sets
Output:
10. “sunionstore” command:
Stores elements that are union of two sets in a new set
Syntax: sunionstore set_name5 set_name1 set_name2
Example: sunionstore Sub FavSub1 FavSub2
Explanation: Sub is a new set created that stores all the unique elements of whole in these 2 sets
Output:
11. “srem” command:
Removes a particular element from given set
Syntax: srem set_name element
Example: srem Sub Math’s
Explanation: Removed Math’s element from the set “Sub”, hence output after executing srem commands is Data Science and Computer Science.
Output:
Complete tutorial on Sets in Redis
In Redis, sets are collections of unique elements, where each element is unique and the order of elements doesn’t matter, as they are unordered. They are handy and useful while working with unique items. Sets are a great way to store and manage various unique items. In Redis, there are several commands for using sets that have their own use cases.
Important Topics for Sets in Redis
- Installation & getting started with Redis
- Syntax and Commands of Redis Sets
- Performance of Redis sets:
- Conclusion
Contact Us