Bird
Raised Fist0
DynamoDBquery~20 mins

UpdateItem basics in DynamoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
DynamoDB UpdateItem Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the result of this UpdateItem operation?

Given a DynamoDB table Users with a primary key UserID and an attribute Age, what will be the new value of Age after running this UpdateItem?

{
  TableName: "Users",
  Key: { "UserID": { "S": "user123" } },
  UpdateExpression: "SET Age = Age + :inc",
  ExpressionAttributeValues: { ":inc": { "N": "5" } },
  ReturnValues: "UPDATED_NEW"
}

Assume the current Age is 30.

DynamoDB
TableName: "Users"
Key: { "UserID": { "S": "user123" } }
UpdateExpression: "SET Age = Age + :inc"
ExpressionAttributeValues: { ":inc": { "N": "5" } }
ReturnValues: "UPDATED_NEW"
A{"Age": 35}
B{"Age": 5}
C{"Age": 30}
DSyntaxError
Attempts:
2 left
💡 Hint

Think about how the SET expression updates the existing value.

📝 Syntax
intermediate
1:30remaining
Which UpdateExpression syntax is valid to set a new attribute?

Which of the following UpdateExpression syntaxes correctly adds a new attribute Status with value Active?

ASET Status = :status
BADD Status = 'Active'
CSET Status = Active
DUPDATE Status = 'Active'
Attempts:
2 left
💡 Hint

Remember to use ExpressionAttributeValues for values in UpdateExpression.

query_result
advanced
2:00remaining
What happens if you try to increment a non-existing numeric attribute?

Consider this UpdateItem request on a DynamoDB table where the attribute Score does not exist yet for the item:

{
  TableName: "Games",
  Key: { "GameID": { "S": "game789" } },
  UpdateExpression: "SET Score = Score + :val",
  ExpressionAttributeValues: { ":val": { "N": "10" } },
  ReturnValues: "UPDATED_NEW"
}

What will be the result?

AScore attribute is created with value 10
BUpdate fails with a ConditionalCheckFailedException
CScore attribute remains undefined
DUpdate fails with a ValidationException
Attempts:
2 left
💡 Hint

Think about how DynamoDB handles arithmetic on missing attributes.

🔧 Debug
advanced
2:00remaining
Why does this UpdateItem request fail with a syntax error?

Examine this UpdateItem snippet:

{
  TableName: "Employees",
  Key: { "EmpID": { "S": "e123" } },
  UpdateExpression: "SET #n = :name",
  ExpressionAttributeNames: { "#n": "Name" },
  ExpressionAttributeValues: { ":name": "John" },
  ReturnValues: "UPDATED_NEW"
}

Why does it fail?

AExpressionAttributeNames key #n is invalid
BExpressionAttributeValues value for :name is missing type wrapper
CUpdateExpression syntax is incorrect
DReturnValues value is invalid
Attempts:
2 left
💡 Hint

Check the format of ExpressionAttributeValues.

🧠 Conceptual
expert
2:30remaining
Which UpdateItem option ensures atomic increment of a numeric attribute?

You want to safely increment a numeric attribute Count in a DynamoDB item without overwriting concurrent updates. Which UpdateItem approach guarantees atomic increment?

ARead the item, increment Count in code, then write back with PutItem
BUse UpdateExpression: "SET Count = Count + :inc" with ExpressionAttributeValues {":inc": {"N": "1"}}
CUse UpdateExpression: "ADD Count :inc" with ExpressionAttributeValues {":inc": {"N": "1"}}
DUse ConditionExpression to check Count before updating with SET
Attempts:
2 left
💡 Hint

Consider DynamoDB's atomic update operators.

Practice

(1/5)
1. What does the UpdateItem operation in DynamoDB do?
easy
A. It reads all items from the table.
B. It deletes the entire item from the table.
C. It creates a new table in DynamoDB.
D. It changes specific attributes of an item without replacing the whole item.

Solution

  1. Step 1: Understand UpdateItem purpose

    The UpdateItem operation is designed to modify parts of an existing item, not the entire item.
  2. Step 2: Compare with other operations

    Deleting an item or creating a table are different operations, so they don't match UpdateItem's function.
  3. Final Answer:

    It changes specific attributes of an item without replacing the whole item. -> Option D
  4. Quick Check:

    UpdateItem modifies parts of an item [OK]
Hint: UpdateItem changes parts, not whole items [OK]
Common Mistakes:
  • Thinking UpdateItem deletes items
  • Confusing UpdateItem with CreateTable
  • Assuming UpdateItem reads data only
2. Which of the following is the correct way to specify an update expression in DynamoDB's UpdateItem?
easy
A. UpdateExpression: 'REMOVE :value'
B. UpdateExpression: 'ADD #name = :value'
C. UpdateExpression: 'SET #name = :value'
D. UpdateExpression: 'DELETE #name = :value'

Solution

  1. Step 1: Identify valid UpdateExpression syntax

    The 'SET' keyword is used to assign a new value to an attribute, correctly written as 'SET #name = :value'.
  2. Step 2: Check other options for syntax errors

    'ADD' requires a space before the value and no '=' sign; 'REMOVE' does not take a value; 'DELETE' syntax is incorrect here.
  3. Final Answer:

    UpdateExpression: 'SET #name = :value' -> Option C
  4. Quick Check:

    Use SET with '=' for updates [OK]
Hint: Use 'SET #attr = :val' for updates [OK]
Common Mistakes:
  • Using '=' with ADD or REMOVE
  • Missing '#' for attribute names
  • Incorrect DELETE syntax
3. Given the following UpdateItem call:
{
  TableName: 'Users',
  Key: { 'UserId': { S: '123' } },
  UpdateExpression: 'SET Age = Age + :inc',
  ExpressionAttributeValues: { ':inc': { N: '1' } }
}

What will happen to the Age attribute of the item with UserId '123'?
medium
A. The operation will fail with a syntax error.
B. Age will be incremented by 1.
C. Age will be deleted.
D. Age will be set to 1.

Solution

  1. Step 1: Analyze the UpdateExpression

    The expression 'SET Age = Age + :inc' means increase the current Age by the value of ':inc', which is 1.
  2. Step 2: Understand the effect on the item

    This will add 1 to the existing Age attribute, not replace or delete it.
  3. Final Answer:

    Age will be incremented by 1. -> Option B
  4. Quick Check:

    SET with addition increments attribute [OK]
Hint: SET with '+ :value' adds to number attributes [OK]
Common Mistakes:
  • Thinking Age resets to 1
  • Assuming Age is deleted
  • Expecting syntax error from addition
4. You wrote this UpdateItem call:
{
  TableName: 'Products',
  Key: { 'ProductId': { S: 'p001' } },
  UpdateExpression: 'SET Count = :newCount',
  ExpressionAttributeValues: { ':newCount': { N: '20' } },
  ExpressionAttributeNames: { '#Count': 'Count' }
}

But the update fails. What is the likely error?
medium
A. You used ExpressionAttributeNames but did not use '#Count' in UpdateExpression.
B. The Key is missing the sort key attribute.
C. The value ':newCount' must be a string, not a number.
D. UpdateExpression cannot use SET to update numeric attributes.

Solution

  1. Step 1: Check usage of ExpressionAttributeNames

    You defined '#Count' in ExpressionAttributeNames but did not use '#Count' in UpdateExpression; you used 'Count' directly instead.
  2. Step 2: Understand attribute name substitution

    When you define ExpressionAttributeNames, you must use the placeholder '#Count' in UpdateExpression to avoid reserved word conflicts.
  3. Final Answer:

    You used ExpressionAttributeNames but did not use '#Count' in UpdateExpression. -> Option A
  4. Quick Check:

    Use '#name' in UpdateExpression if defined [OK]
Hint: Use '#attr' in UpdateExpression if defined [OK]
Common Mistakes:
  • Ignoring ExpressionAttributeNames usage
  • Assuming numeric values must be strings
  • Thinking SET can't update numbers
5. You want to remove the attribute Discount from an item with OrderId '789' using UpdateItem. Which is the correct UpdateExpression and parameters?
hard
A. UpdateExpression: 'REMOVE Discount', Key: { 'OrderId': { S: '789' } }
B. UpdateExpression: 'SET Discount = :null', ExpressionAttributeValues: { ':null': { NULL: true } }
C. UpdateExpression: 'DELETE Discount', Key: { 'OrderId': { S: '789' } }
D. UpdateExpression: 'REMOVE :Discount', ExpressionAttributeValues: { ':Discount': { S: 'Discount' } }

Solution

  1. Step 1: Identify correct syntax to remove attribute

    The 'REMOVE' keyword followed by the attribute name removes that attribute from the item.
  2. Step 2: Check other options for errors

    UpdateExpression: 'SET Discount = :null', ExpressionAttributeValues: { ':null': { NULL: true } } tries to set attribute to null which does not remove it; UpdateExpression: 'DELETE Discount', Key: { 'OrderId': { S: '789' } } uses DELETE incorrectly; UpdateExpression: 'REMOVE :Discount', ExpressionAttributeValues: { ':Discount': { S: 'Discount' } } uses placeholder incorrectly with REMOVE.
  3. Final Answer:

    UpdateExpression: 'REMOVE Discount', Key: { 'OrderId': { S: '789' } } -> Option A
  4. Quick Check:

    Use REMOVE with attribute name to delete attribute [OK]
Hint: Use REMOVE attributeName to delete attribute [OK]
Common Mistakes:
  • Using SET with NULL to remove attribute
  • Using DELETE keyword incorrectly
  • Using placeholders with REMOVE without #