Bird
Raised Fist0
Redisquery~10 mins

SCARD for set size in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - SCARD for set size
Start
↓
Receive SCARD command with key
↓
Check if key exists
↓
Count members
↓
Return count as result
↓
End
The SCARD command checks if the set key exists, counts its members if yes, or returns 0 if no, then returns the count.
Execution Sample
Redis
SADD myset apple banana cherry
SCARD myset
Add three members to a set 'myset' and then get the count of members using SCARD.
Execution Table
StepCommandActionSet StateOutput
1SADD myset apple banana cherryAdd 'apple', 'banana', 'cherry' to 'myset'{myset: ['apple', 'banana', 'cherry']}3
2SCARD mysetCheck if 'myset' exists{myset: ['apple', 'banana', 'cherry']}3
3SCARD unknownsetCheck if 'unknownset' exists{myset: ['apple', 'banana', 'cherry']}0
💡 SCARD returns the number of members if set exists, otherwise 0 if set does not exist.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
mysetundefined['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']['apple', 'banana', 'cherry']
unknownsetundefinedundefinedundefinedundefined
Key Moments - 2 Insights
Why does SCARD return 0 for a key that does not exist?
Because SCARD checks if the set exists first (see execution_table step 3). If the set is missing, it returns 0 instead of an error.
Does SCARD change the set contents?
No, SCARD only counts members without modifying the set, as shown in execution_table step 2 where the set remains unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of SCARD for 'myset' at step 2?
A3
B0
CError
D1
💡 Hint
Check the Output column at step 2 in the execution_table.
At which step does the set 'myset' get its members added?
AStep 3
BStep 1
CStep 2
DNo step adds members
💡 Hint
Look at the Command and Action columns in execution_table step 1.
If the set 'unknownset' was created with 2 members before step 3, what would SCARD return at step 3?
AError
B0
C2
D1
💡 Hint
SCARD returns the count of members if the set exists, see execution_table step 3.
Concept Snapshot
SCARD key
- Returns the number of members in the set stored at key.
- If key does not exist, returns 0.
- Does not modify the set.
- Useful to quickly get set size.
- Example: SCARD myset -> returns count.
Full Transcript
The SCARD command in Redis returns the number of members in a set stored at a given key. When you run SCARD with a key, Redis first checks if the set exists. If it does, it counts the members and returns that number. If the set does not exist, SCARD returns 0 instead of an error. For example, after adding three members to 'myset', SCARD myset returns 3. If you ask for a set that does not exist, like 'unknownset', SCARD returns 0. SCARD does not change the set contents; it only counts members. This makes it a safe and fast way to get the size of a set.

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