Bird
Raised Fist0
Redisquery~5 mins

SCARD for set size in Redis - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the Redis command SCARD do?
The SCARD command returns the number of elements in a set stored at a given key.
Click to reveal answer
beginner
How do you use SCARD to find the size of a set named myset?
You run SCARD myset. This returns the count of elements in the set myset.
Click to reveal answer
beginner
What will SCARD return if the set does not exist?
It returns 0 because a non-existing set is considered empty.
Click to reveal answer
intermediate
Can SCARD be used on data types other than sets?
No, SCARD only works on sets. Using it on other types will cause an error.
Click to reveal answer
beginner
Why is SCARD useful in real life?
It helps quickly find how many unique items are in a group, like counting unique visitors or tags.
Click to reveal answer
What does the Redis command SCARD myset return?
AAdds an element to the set <code>myset</code>
BAll elements in the set <code>myset</code>
CRemoves the set <code>myset</code>
DNumber of elements in the set <code>myset</code>
If the set users does not exist, what does SCARD users return?
A0
BAn error
Cnull
D1
Which data type does SCARD work with?
AStrings
BSets
CHashes
DLists
What happens if you run SCARD on a key holding a string?
AReturns the string length
BReturns 0
CThrows an error
DConverts string to set and returns size
Why might you use SCARD in a web app?
ATo count unique visitors stored in a set
BTo delete user data
CTo add new users
DTo sort user names
Explain what the Redis command SCARD does and give a simple example.
Think about counting unique items in a group.
You got /3 concepts.
    Describe what happens if you use SCARD on a key that is not a set.
    Consider Redis data types and command compatibility.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the Redis command SCARD do when used on a set?
      easy
      A. Returns all elements in the set
      B. Deletes all elements from the set
      C. Adds a new element to the set
      D. Returns the number of unique elements in the set

      Solution

      1. Step 1: Understand the purpose of SCARD

        The SCARD command is designed to count elements in a set, not modify or list them.
      2. Step 2: Identify the correct behavior

        SCARD returns the count of unique elements stored in the set.
      3. Final Answer:

        Returns the number of unique elements in the set -> Option D
      4. Quick Check:

        SCARD = set size count [OK]
      Hint: SCARD counts set items without listing them [OK]
      Common Mistakes:
      • Confusing SCARD with commands that list or delete elements
      • Thinking SCARD adds elements
      • Assuming SCARD returns elements instead of count
      2. Which of the following is the correct syntax to get the size of a set named myset in Redis?
      easy
      A. SCARD myset
      B. SCARD(myset)
      C. GETSIZE myset
      D. SIZE myset

      Solution

      1. Step 1: Recall Redis command syntax

        Redis commands use the format COMMAND key without parentheses.
      2. Step 2: Match correct command for set size

        SCARD is the command to get set size, so SCARD myset is correct.
      3. Final Answer:

        SCARD myset -> Option A
      4. Quick Check:

        Correct Redis syntax = SCARD myset [OK]
      Hint: Redis commands have no parentheses, just command and key [OK]
      Common Mistakes:
      • Using parentheses like a function call
      • Using non-existent commands like GETSIZE or SIZE
      • Confusing SCARD with GET
      3. Given the following Redis commands executed in order:
      SADD fruits apple banana orange
      SADD fruits banana grape
      SCARD fruits

      What will be the output of SCARD fruits?
      medium
      A. 3
      B. 5
      C. 4
      D. 2

      Solution

      1. Step 1: Identify unique elements added to the set

        First SADD adds apple, banana, orange (3 unique). Second SADD adds banana (already exists), grape (new).
      2. Step 2: Count unique elements after both commands

        Unique elements are apple, banana, orange, grape = 4 total.
      3. Final Answer:

        4 -> Option C
      4. Quick Check:

        Unique fruits count = 4 [OK]
      Hint: SCARD counts unique items, duplicates ignored [OK]
      Common Mistakes:
      • Counting duplicates multiple times
      • Assuming SADD overwrites set
      • Confusing SCARD output with list of elements
      4. You run the command SCARD myset but get an error: WRONGTYPE Operation against a key holding the wrong kind of value. What is the most likely cause?
      medium
      A. myset is not a set but another data type like string or list
      B. The key myset does not exist
      C. SCARD command is misspelled
      D. Redis server is down

      Solution

      1. Step 1: Understand the error message

        The error indicates the key exists but is not a set type.
      2. Step 2: Identify cause of WRONGTYPE error

        SCARD only works on sets; if myset is string or list, this error occurs.
      3. Final Answer:

        myset is not a set but another data type like string or list -> Option A
      4. Quick Check:

        WRONGTYPE = wrong data type for SCARD [OK]
      Hint: WRONGTYPE means key is not a set [OK]
      Common Mistakes:
      • Assuming key does not exist causes this error
      • Thinking SCARD command spelling causes this error
      • Ignoring data type mismatch
      5. You have two sets in Redis: setA with 3 elements and setB with 4 elements, where 2 elements overlap. You want to find the total number of unique elements across both sets without retrieving all elements. Which Redis command or combination should you use?
      hard
      A. SCARD setA + SCARD setB
      B. SUNIONSTORE tempSet setA setB followed by SCARD tempSet
      C. SINTER setA setB
      D. SDIFF setA setB

      Solution

      1. Step 1: Understand the problem of counting unique elements across two sets

        Simply adding SCARD results counts duplicates twice.
      2. Step 2: Use SUNIONSTORE to combine sets uniquely

        SUNIONSTORE creates a new set with all unique elements from both sets.
      3. Step 3: Use SCARD on the combined set

        SCARD on the new set gives total unique elements count.
      4. Final Answer:

        SUNIONSTORE tempSet setA setB followed by SCARD tempSet -> Option B
      5. Quick Check:

        Use SUNIONSTORE + SCARD for unique combined count [OK]
      Hint: Combine sets with SUNIONSTORE, then SCARD the result [OK]
      Common Mistakes:
      • Adding SCARD counts directly causing duplicates
      • Using SINTER or SDIFF which don't give total unique count
      • Trying to count without combining sets
      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