Bird
Raised Fist0
DynamoDBquery~20 mins

GetItem (reading single item) 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 GetItem Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this GetItem query?

Given a DynamoDB table Users with primary key UserID, what will be the output of this GetItem operation?

{
  TableName: "Users",
  Key: { "UserID": { "S": "123" } }
}

Assume the item with UserID "123" exists and has attributes Name: "Alice" and Age: 30.

DynamoDB
const params = {
  TableName: "Users",
  Key: { "UserID": { "S": "123" } }
};

// Assume dynamodbClient.getItem(params) returns the item
A{"Item": {"UserID": "123", "Name": "Alice", "Age": 30}}
B{"Item": {"UserID": {"S": "123"}, "Name": {"S": "Alice"}, "Age": {"N": "30"}}}
C{"Items": [{"UserID": {"S": "123"}, "Name": {"S": "Alice"}, "Age": {"N": "30"}}]}
D{"Item": null}
Attempts:
2 left
💡 Hint

Remember DynamoDB returns attribute values with their types.

📝 Syntax
intermediate
2:00remaining
Which GetItem request syntax is valid?

Which of the following DynamoDB GetItem request objects is syntactically correct?

A{ TableName: "Products", Key: { "ProductID": { "S": "001" } } }
B{ TableName: "Products", Key: { ProductID: "001" } }
C{ TableName: "Products", Key: { "ProductID": "001" } }
D{ TableName: "Products", Key: { ProductID: { S: 001 } } }
Attempts:
2 left
💡 Hint

Check how DynamoDB expects attribute values to be typed.

optimization
advanced
2:00remaining
How to optimize a GetItem request to return only specific attributes?

You want to get only the Name attribute from a DynamoDB item with UserID "789". Which GetItem request achieves this efficiently?

A{ TableName: "Users", Key: { "UserID": { "S": "789" } } }
B{ TableName: "Users", Key: { "UserID": { "S": "789" } }, ProjectionExpression: "UserID, Name" }
C{ TableName: "Users", Key: { "UserID": { "S": "789" } }, ProjectionExpression: "Name" }
D{ TableName: "Users", Key: { "UserID": { "S": "789" } }, AttributesToGet: ["Name"] }
Attempts:
2 left
💡 Hint

Use the recommended way to specify attributes to return in GetItem.

🔧 Debug
advanced
2:00remaining
Why does this GetItem request return no item?

Consider this GetItem request:

{
  TableName: "Orders",
  Key: { "OrderID": { "N": 1001 } }
}

The item with OrderID 1001 exists, but the response returns Item: null. Why?

AThe number value must be passed as a string, e.g., { "N": "1001" }.
BThe key attribute value should be a string, not a number.
CThe table name is incorrect, causing no match.
DGetItem cannot query numeric keys.
Attempts:
2 left
💡 Hint

Check how DynamoDB expects number attribute values in requests.

🧠 Conceptual
expert
2:00remaining
What happens if you call GetItem with a non-existent key?

If you call DynamoDB's GetItem with a key that does not exist in the table, what will the response contain?

AThe response returns an item with all attributes set to null.
BThe response throws a <code>ResourceNotFoundException</code> error.
CThe response returns an empty object <code>{}</code> with no <code>Item</code> key.
DThe response contains <code>Item: null</code> indicating no matching item.
Attempts:
2 left
💡 Hint

Think about how DynamoDB signals no matching item found.

Practice

(1/5)
1. What does the GetItem operation in DynamoDB do?
easy
A. Retrieves a single item by its primary key from a table
B. Deletes multiple items from a table
C. Updates all items in a table
D. Scans the entire table for matching items

Solution

  1. Step 1: Understand the purpose of GetItem

    GetItem is designed to fetch exactly one item using its primary key, making it efficient for single record retrieval.
  2. Step 2: Compare with other operations

    Other operations like Scan or Update affect multiple items or the whole table, unlike GetItem which targets one item.
  3. Final Answer:

    Retrieves a single item by its primary key from a table -> Option A
  4. Quick Check:

    GetItem = single item fetch [OK]
Hint: GetItem always fetches one item by key [OK]
Common Mistakes:
  • Confusing GetItem with Scan or Query
  • Thinking GetItem updates or deletes items
  • Assuming GetItem returns multiple items
2. Which of the following is the correct syntax to call GetItem in AWS SDK for JavaScript v3?
easy
A. client.getItem({ TableName: 'Users', Key: { UserId: '123' } })
B. client.getItem({ Table: 'Users', Key: { UserId: '123' } })
C. client.get({ TableName: 'Users', Key: { UserId: '123' } })
D. client.getItem({ TableName: 'Users', Keys: { UserId: '123' } })

Solution

  1. Step 1: Identify correct parameter names

    The correct parameter for the table is TableName and the key attribute is Key (singular), not Keys.
  2. Step 2: Check method name and parameters

    The method is getItem, and the object must include TableName and Key with the primary key value.
  3. Final Answer:

    client.getItem({ TableName: 'Users', Key: { UserId: '123' } }) -> Option A
  4. Quick Check:

    Correct method and parameters = client.getItem({ TableName: 'Users', Key: { UserId: '123' } }) [OK]
Hint: Use TableName and Key (singular) in getItem call [OK]
Common Mistakes:
  • Using 'Table' instead of 'TableName'
  • Using 'Keys' instead of 'Key'
  • Calling method 'get' instead of 'getItem'
3. Given the DynamoDB table 'Products' with primary key 'ProductId', what will the following GetItem call return if the item exists?
client.getItem({ TableName: 'Products', Key: { ProductId: 'P100' } })
medium
A. An error because the key attribute is missing
B. An empty object because GetItem returns no data
C. The item with ProductId 'P100' including all its attributes
D. All items in the 'Products' table

Solution

  1. Step 1: Understand GetItem behavior

    GetItem returns the item matching the exact primary key if it exists, including all attributes stored.
  2. Step 2: Analyze the given call

    The call specifies the correct table and key, so it will return the full item with ProductId 'P100'.
  3. Final Answer:

    The item with ProductId 'P100' including all its attributes -> Option C
  4. Quick Check:

    GetItem returns matching item data [OK]
Hint: GetItem returns full item if key matches [OK]
Common Mistakes:
  • Expecting GetItem to return multiple items
  • Thinking GetItem returns empty if item exists
  • Confusing GetItem with Scan or Query
4. You wrote this code to get an item from DynamoDB but it throws an error:
client.getItem({ TableName: 'Orders', Key: { orderId: 'O123' } })

What is the most likely cause?
medium
A. The client object does not support getItem method
B. The TableName should be 'orders' in lowercase
C. GetItem requires a list of keys, not a single key
D. The key attribute name 'orderId' does not match the table's primary key name

Solution

  1. Step 1: Check key attribute name case sensitivity

    DynamoDB keys are case sensitive and must exactly match the primary key attribute name defined in the table schema.
  2. Step 2: Identify mismatch in key name

    If the table's primary key is 'OrderId' (capital O), using 'orderId' (lowercase o) causes an error because the key is incorrect.
  3. Final Answer:

    The key attribute name 'orderId' does not match the table's primary key name -> Option D
  4. Quick Check:

    Key names must match exactly [OK]
Hint: Match key attribute names exactly, including case [OK]
Common Mistakes:
  • Ignoring case sensitivity in key names
  • Assuming TableName is case insensitive
  • Thinking GetItem needs multiple keys
5. You want to fetch a single item from a DynamoDB table 'Employees' where the primary key is a composite key: DepartmentId (partition key) and EmployeeId (sort key). Which GetItem call is correct?
hard
A. client.getItem({ TableName: 'Employees', Key: { EmployeeId: 'E123' } })
B. client.getItem({ TableName: 'Employees', Key: { DepartmentId: 'D01', EmployeeId: 'E123' } })
C. client.getItem({ TableName: 'Employees', Key: { DepartmentId: 'D01' } })
D. client.getItem({ TableName: 'Employees', Key: { 'D01', 'E123' } })

Solution

  1. Step 1: Understand composite primary key requirements

    For tables with composite keys, both partition key and sort key must be provided in the Key object to uniquely identify the item.
  2. Step 2: Analyze the Key parameter

    The correct call must include both DepartmentId (partition key) and EmployeeId (sort key) as named properties in the Key object. Providing only one key or incorrect syntax like non-named values will fail.
  3. Final Answer:

    client.getItem({ TableName: 'Employees', Key: { DepartmentId: 'D01', EmployeeId: 'E123' } }) -> Option B
  4. Quick Check:

    Composite key requires both keys in Key object [OK]
Hint: Provide all key attributes for composite keys in Key [OK]
Common Mistakes:
  • Providing only one key attribute for composite key
  • Using incorrect syntax for Key object
  • Assuming GetItem works without full key