Bird
Raised Fist0
Redisquery~30 mins

SUNION, SINTER, SDIFF set operations in Redis - Mini Project: Build & Apply

Choose your learning style10 modes available
Using SUNION, SINTER, and SDIFF Set Operations in Redis
📖 Scenario: You are managing a Redis database for a small online bookstore. You have sets representing different categories of books that customers like. You want to find combined, common, and unique interests among these categories.
🎯 Goal: Build Redis commands to create sets for book categories and use SUNION, SINTER, and SDIFF to find combined, common, and unique books liked by customers.
📋 What You'll Learn
Create three Redis sets named fiction, mystery, and science with specified book titles.
Create a variable to hold the set names for operations.
Use SUNION to find all unique books liked in any category.
Use SINTER to find books liked in all categories.
Use SDIFF to find books liked in fiction but not in mystery or science.
💡 Why This Matters
🌍 Real World
Managing user preferences or categories in Redis sets is common in recommendation systems and online stores.
💼 Career
Understanding Redis set operations helps in building fast, efficient data queries for real-time applications.
Progress0 / 4 steps
1
Create Redis sets for book categories
Create three Redis sets named fiction, mystery, and science with these exact members: fiction has 'The Great Gatsby', '1984', 'To Kill a Mockingbird'; mystery has 'Gone Girl', 'The Girl with the Dragon Tattoo', '1984'; science has 'A Brief History of Time', 'The Selfish Gene', '1984'. Use the SADD command for each set.
Redis
Hint

Use SADD followed by the set name and the exact book titles in quotes.

2
Create a variable holding the set names
Create a Redis variable named categories that holds the names of the three sets: fiction, mystery, and science. Use the Redis command SET with the value as a space-separated string of the set names.
Redis
Hint

Use SET categories "fiction mystery science" to store the set names as a string.

3
Use SUNION and SINTER to find combined and common books
Use the SUNION command with the sets fiction, mystery, and science to find all unique books liked by customers. Then use the SINTER command with the same sets to find books liked in all three categories.
Redis
Hint

Use SUNION fiction mystery science and SINTER fiction mystery science commands.

4
Use SDIFF to find unique fiction books
Use the SDIFF command with fiction as the first set and mystery and science as the sets to subtract. This finds books liked in fiction but not in mystery or science.
Redis
Hint

Use SDIFF fiction mystery science to find unique fiction books.

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