How to Use SINTER in Redis: Syntax and Examples
Use the
SINTER command in Redis to find the common members between two or more sets. It returns the members that exist in all specified sets as a result.Syntax
The SINTER command syntax is simple. You provide the names of two or more sets, and Redis returns the members common to all those sets.
SINTER key1 key2 [key3 ...]: Returns the intersection of the sets stored at the given keys.
If any key does not exist, it is treated as an empty set, so the result will be empty.
redis
SINTER key1 key2 [key3 ...]
Example
This example shows how to use SINTER to find common elements between two sets named fruits and tropical.
redis
SADD fruits apple banana orange mango SADD tropical mango pineapple banana SINTER fruits tropical
Output
1) "banana"
2) "mango"
Common Pitfalls
Common mistakes when using SINTER include:
- Using keys that do not exist, which results in an empty intersection.
- Confusing
SINTERwithSUNIONwhich returns all unique members from all sets. - Expecting
SINTERto modify the sets; it only returns the result without changing the original sets.
redis
SINTER fruits unknown_set # Correct usage with existing sets SINTER fruits tropical
Quick Reference
| Command | Description |
|---|---|
| SINTER key1 key2 ... | Returns members common to all specified sets |
| SADD key member [member ...] | Adds members to a set |
| SMEMBERS key | Returns all members of a set |
| SUNION key1 key2 ... | Returns all unique members from all sets |
Key Takeaways
SINTER returns members common to all given sets without modifying them.
If any set key does not exist, the result is an empty set.
Use SADD to add members before using SINTER to find intersections.
Do not confuse SINTER with SUNION; they serve different purposes.