0
0
Redisquery~5 mins

SUNION, SINTER, SDIFF set operations in Redis

Choose your learning style9 modes available
Introduction
These commands help you combine or compare groups of unique items stored in sets, making it easy to find common, different, or all items between them.
You want to find all unique friends from two different friend lists.
You need to find common interests between two groups of users.
You want to see which items are in one list but not in another.
You want to merge tags from multiple articles without duplicates.
You want to filter out items that appear in both sets.
Syntax
Redis
SUNION key1 [key2 ...]
SINTER key1 [key2 ...]
SDIFF key1 [key2 ...]
Each command works with one or more set keys stored in Redis.
The result is a set of unique items based on the operation.
Examples
Returns all unique members from both setA and setB combined.
Redis
SUNION setA setB
Returns only the members that exist in both setA and setB.
Redis
SINTER setA setB
Returns members in setA that are not in setB.
Redis
SDIFF setA setB
Sample Program
First, we add fruits to two sets. Then we find all unique fruits (union), common fruits (intersection), and fruits only in setA (difference).
Redis
SADD setA apple banana cherry
SADD setB banana cherry date
SUNION setA setB
SINTER setA setB
SDIFF setA setB
OutputSuccess
Important Notes
SUNION combines all unique elements from the given sets.
SINTER finds only elements present in every set.
SDIFF returns elements from the first set that are missing in the others.
Summary
SUNION merges sets and removes duplicates.
SINTER finds common elements between sets.
SDIFF finds elements unique to the first set.