Bird
Raised Fist0
DynamoDBquery~20 mins

SET expression for adding/changing 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 SET Expression 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 SET expression update?
Given a DynamoDB item with attributes {"id": "123", "count": 5, "tags": ["blue"]}, what will be the updated item after applying this UpdateExpression?

SET count = count + :inc, tags = list_append(tags, :newTags)

with ExpressionAttributeValues:
{":inc": 3, ":newTags": ["green"]}
A{"id": "123", "count": 8, "tags": ["green", "blue"]}
B{"id": "123", "count": 3, "tags": ["green"]}
C{"id": "123", "count": 8, "tags": ["blue", "green"]}
D{"id": "123", "count": 5, "tags": ["blue"]}
Attempts:
2 left
💡 Hint
Remember that SET can add numbers and list_append appends to the end of a list.
📝 Syntax
intermediate
1:30remaining
Which SET expression is syntactically correct for updating multiple attributes?
Choose the correct DynamoDB UpdateExpression to set attribute 'status' to 'active' and increment 'score' by 10.
ASET status = :s, score = score + :inc
BSET status := :s, score += :inc
CSET status = :s; score = score + :inc
DSET status == :s, score = score + :inc
Attempts:
2 left
💡 Hint
Use single equals '=' for assignment and '+' for addition in SET expressions.
optimization
advanced
2:30remaining
Optimizing a SET expression to update nested attributes
You want to update the nested attribute 'address.city' to 'Seattle' and increment 'visits' by 1 in a DynamoDB item. Which UpdateExpression is the most efficient and correct?
ASET address.city = :city, visits = visits + :one
BSET #addr.#city = :city, visits = visits + :one
CSET address['city'] = :city, visits = visits + :one
DSET address.city := :city, visits += :one
Attempts:
2 left
💡 Hint
Use ExpressionAttributeNames for nested attributes to avoid reserved word conflicts.
🔧 Debug
advanced
2:00remaining
Why does this SET expression cause an error?
Given this UpdateExpression:
SET count = count + :inc, tags = list_append(:newTags, tags)
and ExpressionAttributeValues:
{":inc": 2, ":newTags": ["red"]}
The update fails with a ValidationException. Why?
Alist_append requires the existing list as the first argument, but :newTags is first here.
BThe syntax of list_append is incorrect; it needs three arguments.
CThe ExpressionAttributeValues are missing a value for :tags.
DThe attribute 'count' does not exist, so it cannot be incremented.
Attempts:
2 left
💡 Hint
Check the order of arguments in list_append function.
🧠 Conceptual
expert
2:30remaining
What happens when you use SET to add a new attribute that already exists?
If you run this UpdateExpression:
SET newAttr = if_not_exists(newAttr, :start) + :inc
with ExpressionAttributeValues:
{":start": 0, ":inc": 5}
and the item already has newAttr = 10, what will be the new value of newAttr?
A5
B0
CAn error occurs because newAttr already exists.
D15
Attempts:
2 left
💡 Hint
if_not_exists returns the attribute value if it exists, otherwise the default.

Practice

(1/5)
1. What does the SET expression do in a DynamoDB UpdateItem operation?
easy
A. It creates a new table.
B. It adds new attributes or updates existing attributes in an item.
C. It retrieves items from the table.
D. It deletes attributes from an item.

Solution

  1. Step 1: Understand the purpose of SET in UpdateItem

    The SET expression is used to add new attributes or change existing ones in a DynamoDB item.
  2. Step 2: Compare with other operations

    Deleting attributes uses REMOVE, retrieving uses GetItem or Query, and creating tables uses CreateTable, not SET.
  3. Final Answer:

    It adds new attributes or updates existing attributes in an item. -> Option B
  4. Quick Check:

    SET expression = add or update attributes [OK]
Hint: SET means add or change attributes in DynamoDB [OK]
Common Mistakes:
  • Confusing SET with REMOVE for deleting attributes
  • Thinking SET retrieves data
  • Assuming SET creates tables
2. Which of the following is the correct syntax for a SET expression to update an attribute named age to 30?
easy
A. SET age == 30
B. SET age := 30
C. SET age -> 30
D. SET age = 30

Solution

  1. Step 1: Recall correct SET syntax

    In DynamoDB, the SET expression uses a single equals sign (=) to assign new values.
  2. Step 2: Check each option

    SET age = 30 uses 'SET age = 30' which is correct. Options A, B, and D use invalid operators for assignment.
  3. Final Answer:

    SET age = 30 -> Option D
  4. Quick Check:

    Assignment uses = in SET expression [OK]
Hint: Use single = for assignment in SET expression [OK]
Common Mistakes:
  • Using := or == instead of =
  • Using arrows or other symbols
  • Missing the SET keyword
3. Given the following UpdateItem call:
UpdateExpression: "SET #n = :newName, age = age + :inc",
ExpressionAttributeNames: {"#n": "name"},
ExpressionAttributeValues: {":newName": "Alice", ":inc": 1}

What will be the effect on the item?
medium
A. The attribute 'name' is set to 'Alice' and 'age' is incremented by 1.
B. The attribute 'name' is set to ':newName' and 'age' is set to ':inc'.
C. The attribute 'name' is deleted and 'age' is unchanged.
D. Syntax error due to missing commas.

Solution

  1. Step 1: Understand the UpdateExpression

    The expression sets the attribute 'name' (aliased as #n) to the value ':newName' which is 'Alice'. It also increments 'age' by the value ':inc' which is 1.
  2. Step 2: Confirm syntax and effect

    The syntax is correct with commas separating updates. So, 'name' becomes 'Alice' and 'age' increases by 1.
  3. Final Answer:

    The attribute 'name' is set to 'Alice' and 'age' is incremented by 1. -> Option A
  4. Quick Check:

    SET updates multiple attributes correctly [OK]
Hint: Commas separate multiple SET updates; placeholders map values [OK]
Common Mistakes:
  • Confusing placeholders with literal strings
  • Missing commas between updates
  • Not using ExpressionAttributeNames for reserved words
4. Identify the error in this UpdateExpression:
SET age = age + :inc newName = :bob
medium
A. Using double quotes inside expression values.
B. Incorrect use of colon before newName.
C. Missing comma between updates.
D. Using plus sign for addition is invalid.

Solution

  1. Step 1: Analyze the UpdateExpression syntax

    The expression tries to update 'age' and 'newName' but lacks a comma between the two updates.
  2. Step 2: Check other syntax elements

    Placeholders (:inc, :bob) are used correctly for values. Attribute names (age, newName) do not use colons. The plus sign is valid for numeric addition.
  3. Final Answer:

    Missing comma between updates. -> Option C
  4. Quick Check:

    Separate multiple SET updates with commas [OK]
Hint: Separate multiple SET updates with commas [OK]
Common Mistakes:
  • Omitting commas between attribute updates
  • Incorrectly placing colons before attribute names
  • Misplacing colons in attribute names
5. You want to update a DynamoDB item to set the attribute status to "active" only if it does not exist, and also increment loginCount by 1. Which UpdateExpression correctly does this?
hard
A. SET status = if_not_exists(status, :active), loginCount = loginCount + :inc
B. SET status = :active, loginCount = loginCount + 1 IF NOT EXISTS status
C. SET status = :active IF NOT EXISTS status, loginCount = loginCount + :inc
D. SET status = :active, loginCount = loginCount + :inc WHERE status = NULL

Solution

  1. Step 1: Understand conditional setting with if_not_exists

    The function if_not_exists(attribute, value) sets the attribute only if it does not exist, which matches the requirement for 'status'.
  2. Step 2: Check increment syntax and placeholders

    Incrementing 'loginCount' by ':inc' is correct. SET status = if_not_exists(status, :active), loginCount = loginCount + :inc uses placeholders properly and combines both updates in one SET expression.
  3. Step 3: Evaluate other options

    Options B, C, and D use invalid syntax like 'IF NOT EXISTS' outside functions or WHERE clauses which are not valid in UpdateExpression.
  4. Final Answer:

    SET status = if_not_exists(status, :active), loginCount = loginCount + :inc -> Option A
  5. Quick Check:

    Use if_not_exists() to set only if missing [OK]
Hint: Use if_not_exists() to set attribute only if missing [OK]
Common Mistakes:
  • Trying to use IF NOT EXISTS outside if_not_exists() function
  • Using WHERE clause in UpdateExpression
  • Not using placeholders for values