In Redis, the SINTER
command returns the members of the set resulting from the intersection of all the given sets.
Syntax
The syntax goes like this:
SINTER key [key ...]
Example
Suppose we create the following sets:
SADD set1 0 1 2 3 4 5 6
SADD set2 5 6 7 8
We can use the SINTER
command to return the members of the set resulting from the intersection of both sets:
SINTER set1 set2
Result:
1) "5" 2) "6"
We get 5
and 6
, because those two members are in both sets.
Now let’s create another set:
SADD set3 1 4 5
Now let’s include that as a set when calling the SINTER
command:
SINTER set1 set2 set3
Result:
1) "5"
This time only 5
is returned. That’s because it’s the only member that’s common to all sets.
Let’s remove the second set:
SINTER set1 set3
Result:
1) "1" 2) "4" 3) "5"
More values are returned this time.
Now let’s create a fourth set, but this time with values that aren’t common to all other sets:
SADD set4 0 1 7 8
And pass all sets to the SINTER
command:
SINTER set1 set2 set3 set4
Result:
(empty array)
We get an empty array, because none of the members are common across all sets.
Non-Existent Keys
Keys that don’t exist are treated as empty sets. If we pass an empty set to the SINTER
command, the result will be an empty array. That is because any intersection with an empty set will always result in an empty set.
Example:
SINTER set1 nonexistentkey
Result:
(empty array)