Bird
Raised Fist0
Redisquery~10 mins

HKEYS and HVALS in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - HKEYS and HVALS
Start with a Redis hash
↓
Run HKEYS command
→Get all field names
↓
Run HVALS command
→Get all field values
↓
Use results as needed
↓
End
You start with a Redis hash, then use HKEYS to get all field names and HVALS to get all field values separately.
Execution Sample
Redis
HSET user:1 name "Alice" age "30" city "NY"
HKEYS user:1
HVALS user:1
This sets a hash with fields name, age, city, then retrieves all keys and all values.
Execution Table
StepCommandActionResult
1HSET user:1 name "Alice" age "30" city "NY"Create hash user:1 with fields and values3
2HKEYS user:1Retrieve all field names from user:1["name", "age", "city"]
3HVALS user:1Retrieve all field values from user:1["Alice", "30", "NY"]
4EndNo more commandsExecution complete
💡 All commands executed; HKEYS and HVALS returned all fields and values respectively.
Variable Tracker
VariableStartAfter HSETAfter HKEYSAfter HVALSFinal
user:1empty{"name":"Alice", "age":"30", "city":"NY"}["name", "age", "city"]["Alice", "30", "NY"]unchanged
Key Moments - 2 Insights
Why do HKEYS and HVALS return separate lists instead of pairs?
HKEYS returns only the field names and HVALS returns only the values. To get pairs, you use HGETALL. See execution_table rows 2 and 3.
Does the order of fields in HKEYS match the order of values in HVALS?
Yes, the order of fields from HKEYS matches the order of values from HVALS, so you can pair them by position. See execution_table rows 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does HKEYS user:1 return at step 2?
A["user:1"]
B["Alice", "30", "NY"]
C["name", "age", "city"]
DAn error
💡 Hint
Check the 'Result' column in row 2 of the execution_table.
At which step do we get the list of all values in the hash?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the command HVALS in the execution_table.
If the hash user:1 had an extra field 'email', how would HKEYS output change?
AIt would include 'email' in the list
BIt would exclude 'email'
CIt would return values instead of keys
DIt would cause an error
💡 Hint
HKEYS returns all field names present in the hash as shown in execution_table row 2.
Concept Snapshot
HKEYS key
  - Returns all field names in the hash stored at key.
HVALS key
  - Returns all field values in the hash stored at key.
Order of keys and values matches.
Use HGETALL for key-value pairs.
Both commands return arrays.
Full Transcript
We start with a Redis hash named user:1 with fields name, age, and city set to Alice, 30, and NY respectively. The HKEYS command retrieves all the field names as a list: name, age, city. The HVALS command retrieves all the field values as a list: Alice, 30, NY. The order of keys and values matches, so you can pair them by position. These commands help you get just the keys or just the values from a hash. To get both keys and values together, you would use HGETALL. This trace shows how the hash changes and what each command returns step by step.

Practice

(1/5)
1. What does the Redis command HKEYS myhash return?
easy
A. All the field names (keys) inside the hash myhash
B. All the values inside the hash myhash
C. The number of fields in the hash myhash
D. Deletes the hash myhash

Solution

  1. Step 1: Understand the purpose of HKEYS

    The command HKEYS is used to get all the field names (keys) from a Redis hash.
  2. Step 2: Apply to the given hash

    When you run HKEYS myhash, it returns all the keys inside myhash, not the values or count.
  3. Final Answer:

    All the field names (keys) inside the hash myhash -> Option A
  4. Quick Check:

    HKEYS returns keys = A [OK]
Hint: HKEYS always lists keys, not values or counts [OK]
Common Mistakes:
  • Confusing HKEYS with HVALS
  • Thinking it returns values instead of keys
  • Assuming it returns the count of fields
2. Which of the following is the correct syntax to get all values from a Redis hash named user:100?
easy
A. HVALS user:100
B. HKEYS user:100
C. HGETALL user:100
D. HVALS(user:100)

Solution

  1. Step 1: Identify the command to get all values

    The Redis command to get all values from a hash is HVALS.
  2. Step 2: Check syntax correctness

    The correct syntax is HVALS keyname, so HVALS user:100 is correct. Parentheses are not used in Redis commands.
  3. Final Answer:

    HVALS user:100 -> Option A
  4. Quick Check:

    HVALS syntax = HVALS key [OK]
Hint: HVALS command syntax: HVALS key (no parentheses) [OK]
Common Mistakes:
  • Using parentheses like a function call
  • Mixing HKEYS and HVALS commands
  • Using HGETALL instead of HVALS for values only
3. Given a Redis hash product:1 with fields and values:
{"name": "Pen", "color": "blue", "price": "1.5"}
What will HVALS product:1 return?
medium
A. ["name", "color", "price"]
B. ["Pen", "blue", "1.5"]
C. ["product:1"]
D. An error because HVALS needs a list

Solution

  1. Step 1: Understand what HVALS returns

    HVALS returns all the values stored in the hash fields, not the keys.
  2. Step 2: Apply to the given hash

    The values are "Pen", "blue", and "1.5". So the command returns these values as a list.
  3. Final Answer:

    ["Pen", "blue", "1.5"] -> Option B
  4. Quick Check:

    HVALS returns values list = A [OK]
Hint: HVALS returns only values, not keys or errors [OK]
Common Mistakes:
  • Confusing keys with values
  • Expecting field names instead of values
  • Thinking HVALS returns the hash key name
4. You run HKEYS myhash but get an empty list even though you know myhash has data. What could be the problem?
medium
A. The Redis server is down
B. You typed HVALS myhash instead of HKEYS
C. myhash is not a hash type but a string or other type
D. The hash myhash is empty

Solution

  1. Step 1: Check the data type of the key

    If myhash is not a hash but a string or other type, HKEYS returns an empty list.
  2. Step 2: Understand why other options are incorrect

    Typing HVALS would return values, not empty list. Server down causes connection errors, not empty list. An empty hash would return empty list but question says it has data.
  3. Final Answer:

    myhash is not a hash type but a string or other type -> Option C
  4. Quick Check:

    Wrong type key = empty HKEYS result [OK]
Hint: Empty HKEYS often means key is wrong type, not empty hash [OK]
Common Mistakes:
  • Assuming empty list means empty hash
  • Ignoring key data type
  • Thinking server down returns empty list
5. You want to list all field names and values from a Redis hash session:123 but only want the values that are not empty strings. Which approach using HKEYS and HVALS is best?
hard
A. Use HVALS session:123 and remove empty strings from the returned list
B. Use HKEYS session:123 only, empty values are not returned
C. Use HGETALL session:123 and filter out empty values in your application
D. Use HKEYS session:123 to get keys, then filter values by fetching each with HGET and checking for empty strings

Solution

  1. Step 1: Understand what HKEYS and HVALS return

    HKEYS returns all field names, HVALS returns all values including empty strings.
  2. Step 2: Choose a method to filter empty values

    Since HVALS returns all values without keys, filtering empty strings is hard without knowing which value belongs to which key. Using HKEYS to get keys, then fetching each value with HGET allows filtering empty strings precisely.
  3. Step 3: Evaluate other options

    HVALS alone does not provide keys, so filtering loses context. HGETALL returns all keys and values but question asks about HKEYS and HVALS usage. Using only HKEYS does not get values.
  4. Final Answer:

    Use HKEYS to get keys, then fetch and filter values with HGET -> Option D
  5. Quick Check:

    Filter empty values by keys + HGET = B [OK]
Hint: Get keys first, then fetch values to filter empty strings [OK]
Common Mistakes:
  • Filtering values without keys loses context
  • Assuming HVALS excludes empty strings
  • Using only HKEYS without fetching values
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