0
0
Redisquery~10 mins

SUNION, SINTER, SDIFF set operations in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SUNION, SINTER, SDIFF set operations
Start with Sets A and B
Perform Set Operation
Return Resulting Set
End
Start with two sets, pick one of the operations (union, intersection, difference), perform it, and get the resulting set.
Execution Sample
Redis
SADD setA 1 2 3
SADD setB 2 3 4
SUNION setA setB
SINTER setA setB
SDIFF setA setB
Add elements to two sets, then perform union, intersection, and difference operations.
Execution Table
StepCommandSetASetBOperationResult
1SADD setA 1 2 31,2,3---
2SADD setB 2 3 41,2,32,3,4--
3SUNION setA setB1,2,32,3,4Union1,2,3,4
4SINTER setA setB1,2,32,3,4Intersection2,3
5SDIFF setA setB1,2,32,3,4Difference1
6SDIFF setB setA1,2,32,3,4Difference4
7End1,2,32,3,4--
💡 All operations performed. Sets remain unchanged by SUNION, SINTER, SDIFF (read-only operations).
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
setAempty1,2,31,2,31,2,31,2,31,2,31,2,3
setBemptyempty2,3,42,3,42,3,42,3,42,3,4
Resultnonenonenone1,2,3,42,311 (last result)
Key Moments - 3 Insights
Why does SUNION return all unique elements from both sets?
SUNION combines all elements from both sets without duplicates, as shown in step 3 of the execution_table where the result is 1,2,3,4.
Why does SINTER return only elements present in both sets?
SINTER returns the common elements found in both sets, demonstrated in step 4 where the result is 2,3, which appear in both setA and setB.
Why does SDIFF setA setB return elements only in setA?
SDIFF returns elements in the first set that are not in the second. Step 5 shows result 1, which is in setA but not in setB.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the result of SINTER setA setB?
A2,3
B1,2,3,4
C1
D4
💡 Hint
Check the 'Result' column at step 4 in the execution_table.
At which step does the SDIFF operation return the element '1'?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Operation' and 'Result' columns in the execution_table for SDIFF returning '1'.
If we add element '5' to setB before step 3, how would SUNION result change?
AResult would be empty
BResult would exclude '5'
CResult would include '5'
DResult would be same as before
💡 Hint
SUNION returns all unique elements from both sets, so adding '5' to setB adds it to the union.
Concept Snapshot
SUNION key1 key2 ... - returns all unique elements from all sets combined
SINTER key1 key2 ... - returns elements common to all sets
SDIFF key1 key2 ... - returns elements in first set not in others
All commands operate on Redis sets and return a set result
Sets remain unchanged after these operations
Full Transcript
This visual execution traces Redis set operations SUNION, SINTER, and SDIFF. We start by adding elements to two sets, setA and setB. SUNION combines all unique elements from both sets, shown as 1,2,3,4. SINTER returns only elements present in both sets, which are 2 and 3. SDIFF returns elements in the first set not in the second; for setA minus setB, the result is 1. The sets themselves do not change after these operations. This step-by-step trace helps understand how these set operations work in Redis.