0
0
Redisquery~10 mins

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

Choose your learning style9 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.