Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 modes available
Concept Flow - ZCARD for set size
Start
↓
Call ZCARD with key
↓
Check if key exists
↓
Count elements
↓
Return count
The ZCARD command checks if the sorted set key exists, counts its elements if yes, or returns 0 if no.
Execution Sample
Redis
ZADD myset 1 one 2 two 3 three
ZCARD myset
ZCARD unknownset
Add three elements to a sorted set 'myset', then get its size with ZCARD, and check size of a non-existing set.
Execution Table
StepCommandKeyKey Exists?ActionOutput
1ZADDmysetNoCreate 'myset' and add 3 elements3
2ZCARDmysetYesCount elements in 'myset'3
3ZCARDunknownsetNoReturn 0 because key does not exist0
💡 Execution stops after returning the count or 0 for each ZCARD command.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
mysetdoes not existsorted set with 3 elementsunchangedunchanged
unknownsetdoes not existdoes not existdoes not existdoes not exist
Key Moments - 2 Insights
Why does ZCARD return 0 for a key that does not exist?
Because as shown in execution_table row 3, when the key is missing, Redis returns 0 instead of an error.
Does ZCARD count the number of elements or sum their scores?
ZCARD counts the number of elements only, not their scores, as shown in row 2 where output is 3 for three elements.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of ZCARD for 'myset' at step 2?
AError
B0
C3
D1
💡 Hint
Check the 'Output' column in execution_table row 2.
At which step does Redis create the sorted set 'myset'?
AStep 3
BStep 1
CStep 2
DNo creation happens
💡 Hint
Look at the 'Action' column in execution_table row 1.
If 'unknownset' existed with 5 elements, what would ZCARD return at step 3?
A5
B0
CError
D1
💡 Hint
ZCARD returns the count of elements if the key exists, see execution_table row 2 for example.
Concept Snapshot
ZCARD key
- Returns the number of elements in a sorted set.
- If key does not exist, returns 0.
- Does not sum scores, only counts elements.
- Useful to check size of sorted sets quickly.
Full Transcript
The ZCARD command in Redis returns the number of elements in a sorted set stored at the given key. If the key does not exist, it returns 0 instead of an error. For example, after adding three elements to 'myset', ZCARD 'myset' returns 3. If you call ZCARD on a non-existing key like 'unknownset', it returns 0. This command helps you quickly find out how many members are in a sorted set without retrieving the elements themselves.

Practice

(1/5)
1. What does the Redis command ZCARD do when used on a sorted set?
easy
A. Deletes all elements in the sorted set
B. Returns the highest score in the sorted set
C. Returns the elements with the lowest score
D. Returns the number of elements in the sorted set

Solution

  1. Step 1: Understand the purpose of ZCARD

    The ZCARD command is designed to count how many elements are in a sorted set.
  2. Step 2: Compare with other options

    Other options describe different commands like fetching scores or deleting, which ZCARD does not do.
  3. Final Answer:

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

    ZCARD = count of elements [OK]
Hint: ZCARD counts items in sorted set, not scores or values [OK]
Common Mistakes:
  • Confusing ZCARD with ZRANGE or ZSCORE
  • Thinking it deletes elements
  • Assuming it returns scores instead of count
2. Which of the following is the correct syntax to get the size of a sorted set named myzset in Redis?
easy
A. ZCOUNT myzset
B. ZCARD myzset
C. ZSIZE myzset
D. ZLEN myzset

Solution

  1. Step 1: Recall the correct command name

    The command to get the number of elements in a sorted set is ZCARD.
  2. Step 2: Verify syntax correctness

    The syntax is ZCARD key, so ZCARD myzset is correct.
  3. Final Answer:

    ZCARD myzset -> Option B
  4. Quick Check:

    Correct command syntax = ZCARD myzset [OK]
Hint: Use ZCARD followed by set name to get size [OK]
Common Mistakes:
  • Using ZCOUNT which counts by score range
  • Using non-existent commands like ZSIZE or ZLEN
  • Forgetting to include the key name
3. Given the following Redis commands executed in order:
ZADD fruits 1 apple 2 banana 3 cherry
ZADD fruits 4 date
ZREM fruits banana

What will ZCARD fruits return?
medium
A. 4
B. 1
C. 3
D. 2

Solution

  1. Step 1: Count elements after additions

    After ZADD fruits 1 apple 2 banana 3 cherry, the set has 3 elements: apple, banana, cherry.
  2. Step 2: Add one more element and remove one

    ZADD fruits 4 date adds date, total 4 elements. Then ZREM fruits banana removes banana, leaving 3 elements.
  3. Final Answer:

    3 -> Option C
  4. Quick Check:

    Elements after ops = 3 [OK]
Hint: Additions minus removals = final count [OK]
Common Mistakes:
  • Counting removed elements
  • Ignoring the last removal command
  • Assuming ZADD replaces instead of adds
4. You run ZCARD myset but get an error: WRONGTYPE Operation against a key holding the wrong kind of value. What is the most likely cause?
medium
A. The key 'myset' is not a sorted set but another data type
B. The key 'myset' does not exist
C. The ZCARD command is misspelled
D. Redis server is down

Solution

  1. Step 1: Understand the error message

    The error WRONGTYPE means the key exists but is not the expected data type.
  2. Step 2: Match error to cause

    Since ZCARD works only on sorted sets, the key must be a different type like string or list.
  3. Final Answer:

    The key 'myset' is not a sorted set but another data type -> Option A
  4. Quick Check:

    WRONGTYPE = wrong data type key [OK]
Hint: WRONGTYPE means key is not a sorted set [OK]
Common Mistakes:
  • Assuming key does not exist causes this error
  • Thinking command spelling causes WRONGTYPE
  • Blaming server status for this error
5. You have a sorted set scores with 1000 elements. You want to check if it is empty before processing. Which Redis command and check is the best and most efficient way?
hard
A. Use ZCARD scores and check if result is 0
B. Use ZRANGE scores 0 -1 and check if result is empty
C. Use ZCOUNT scores -inf +inf and check if result is 0
D. Use GET scores and check if result is null

Solution

  1. Step 1: Understand command efficiency

    ZCARD returns the count quickly without fetching all elements, ideal for checking emptiness.
  2. Step 2: Compare other options

    ZRANGE fetches all elements, costly for large sets. ZCOUNT counts by score range but is less direct. GET is for strings, not sorted sets.
  3. Final Answer:

    Use ZCARD scores and check if result is 0 -> Option A
  4. Quick Check:

    ZCARD = fast size check [OK]
Hint: ZCARD quickly returns size, best for emptiness check [OK]
Common Mistakes:
  • Using ZRANGE wastes resources fetching all elements
  • Using GET on a sorted set key causes errors
  • Using ZCOUNT unnecessarily when ZCARD suffices
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