Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the Redis command SUNION do when applied to multiple sets?
easy
A. It returns all unique elements from all given sets combined.
B. It returns only the elements common to all given sets.
C. It returns elements unique to the first set only.
D. It deletes all the given sets from the database.

Solution

  1. Step 1: Understand SUNION operation

    The SUNION command merges all elements from the given sets and removes duplicates.
  2. Step 2: Compare with other set operations

    SINTER returns only common elements, SDIFF returns unique elements from the first set, so SUNION is the only one that merges all uniquely.
  3. Final Answer:

    It returns all unique elements from all given sets combined. -> Option A
  4. Quick Check:

    SUNION = all unique elements combined [OK]
Hint: SUNION combines all sets without duplicates [OK]
Common Mistakes:
  • Confusing SUNION with SINTER (common elements)
  • Thinking SUNION returns only first set elements
  • Assuming SUNION deletes sets
2. Which of the following is the correct syntax to find the intersection of two sets set1 and set2 in Redis?
easy
A. SINTER set1 set2
B. SUNION set1 set2
C. SDIFF set1 set2
D. SINTERSECTION set1 set2

Solution

  1. Step 1: Recall Redis command for intersection

    The correct Redis command to find common elements is SINTER.
  2. Step 2: Check syntax correctness

    The syntax is SINTER key1 key2 .... SINTER set1 set2 matches this exactly.
  3. Final Answer:

    SINTER set1 set2 -> Option A
  4. Quick Check:

    SINTER syntax = SINTER keys [OK]
Hint: Use SINTER for intersection syntax [OK]
Common Mistakes:
  • Using SUNION instead of SINTER
  • Using non-existent command SINTERSECTION
  • Confusing SDIFF with intersection
3. Given the following Redis sets:
set1 = {"apple", "banana", "cherry"}
set2 = {"banana", "date", "fig"}
What is the result of SINTER set1 set2?
medium
A. ["apple", "banana", "cherry", "date", "fig"]
B. ["banana"]
C. ["apple", "cherry"]
D. ["date", "fig"]

Solution

  1. Step 1: Identify common elements between set1 and set2

    Elements in set1: apple, banana, cherry; set2: banana, date, fig. The only common element is banana.
  2. Step 2: Understand SINTER output

    SINTER returns only elements present in all sets, so the result is ["banana"].
  3. Final Answer:

    ["banana"] -> Option B
  4. Quick Check:

    SINTER common elements = ["banana"] [OK]
Hint: SINTER returns only common elements [OK]
Common Mistakes:
  • Listing all elements instead of common ones
  • Confusing SINTER with SUNION or SDIFF
  • Including elements unique to one set
4. You run the command SDIFF set1 set2 but get an unexpected empty result. Given:
set1 = {"cat", "dog"}
set2 = {"cat", "dog", "mouse"}
What is the likely reason?
medium
A. set1 is empty, so SDIFF returns empty.
B. SDIFF syntax requires only one set argument.
C. Redis does not support SDIFF with these sets.
D. All elements of set1 are also in set2, so no unique elements remain.

Solution

  1. Step 1: Understand SDIFF behavior

    SDIFF returns elements in the first set that are not in the other sets.
  2. Step 2: Analyze given sets

    set1 has "cat" and "dog"; set2 has "cat", "dog", and "mouse". Since all set1 elements are in set2, SDIFF returns an empty set.
  3. Final Answer:

    All elements of set1 are also in set2, so no unique elements remain. -> Option D
  4. Quick Check:

    SDIFF unique first set elements = empty [OK]
Hint: SDIFF returns only unique first set elements [OK]
Common Mistakes:
  • Assuming SDIFF requires only one set
  • Thinking SDIFF returns common elements
  • Believing Redis lacks SDIFF support
5. You have three Redis sets:
setA = {"red", "blue", "green"}
setB = {"blue", "yellow"}
setC = {"green", "yellow", "black"}
Which command returns elements that are in setA but not in setB or setC?
hard
A. SUNION setA setB setC
B. SINTER setA setB setC
C. SDIFF setA setB setC
D. SDIFF setB setA setC

Solution

  1. Step 1: Understand SDIFF with multiple sets

    SDIFF returns elements in the first set that are not in any of the other sets.
  2. Step 2: Apply SDIFF to given sets

    Elements in setA: red, blue, green. Elements in setB and setC combined: blue, yellow, green, black. Elements unique to setA are only "red".
  3. Step 3: Verify command correctness

    SDIFF setA setB setC uses SDIFF with setA first, then setB and setC, which matches the requirement.
  4. Final Answer:

    SDIFF setA setB setC -> Option C
  5. Quick Check:

    SDIFF first set minus others = unique elements [OK]
Hint: Use SDIFF with first set then others to find unique elements [OK]
Common Mistakes:
  • Using SINTER or SUNION instead of SDIFF
  • Reversing set order in SDIFF
  • Expecting SDIFF to return common elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard